Remove extra zeros from parameters in the workspace

7 visualizaciones (últimos 30 días)
Paris Pasqualin
Paris Pasqualin el 11 de Jun. de 2023
Comentada: Paris Pasqualin el 11 de Jun. de 2023
Even if my parameters in the command window are displayed with the short format, which I have set from the preferences, when I copy-paste parameters from the workspace they are not in short format. For example, if I have X = 17.2 in the command window, when I copy-paste from the workspace I have 17.2000000000000. I want to eliminate the extra zeros, is there a way to do it?
Thanks

Respuestas (2)

Diwakar Diwakar
Diwakar Diwakar el 11 de Jun. de 2023
You can use the sprintf function or the num2str function to achieve this.
Example
X = 17.2;
formattedX = sprintf('%.1f', X);
disp(formattedX);
17.2
  1 comentario
Paris Pasqualin
Paris Pasqualin el 11 de Jun. de 2023
Thanks for the reply. I actually have a longer array and the command you told me gives "'17.217.217.219.222.022.019.217.219.220.022.019.2'". I want it to be "17.2 17.2 17.2 19.2 22 22 19.2 17.2 19.2 20 22 19.2",
but when I copy-paste the parameter I get "17.2000000000000 17.2000000000000 17.2000000000000 19.1999999999999 22 22 19.1999999999999 17.2000000000000 19.1999999999999 20 22 19.1999999999999"
I hope my comment helps to understand my issue.

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 11 de Jun. de 2023
Without using fprintf(), you can get rid of some of them by putting this in your script at the top.
format short g
  1 comentario
Paris Pasqualin
Paris Pasqualin el 11 de Jun. de 2023
Hi Thanks for the response. I am sorry but I think I am not explaining myself correctly, so I am posting a creenshot. I am already "format shortG" and the values are correctly shown in the command window. My problem is that when I click on the variable under "workshop", it opens it with many digit after the dot (as seen from the right side where it displays "17.199999999999957". So when I copy-paste the variable from the workshop I get "17.199999999999957" even though the same value is 17.2 in the command window. Hope this explaination might help to explain my issue.
Thanks again,
Paris

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by