Remove extra zeros from parameters in the workspace

7 次查看(过去 30 天)
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

回答(2 个)

Diwakar Diwakar
Diwakar Diwakar 2023-6-11
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 个评论
Paris Pasqualin
Paris Pasqualin 2023-6-11
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.

请先登录,再进行评论。


Image Analyst
Image Analyst 2023-6-11
Without using fprintf(), you can get rid of some of them by putting this in your script at the top.
format short g
  1 个评论
Paris Pasqualin
Paris Pasqualin 2023-6-11
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by