Remove extra zeros from parameters in the workspace

조회 수: 7 (최근 30일)
Paris Pasqualin
Paris Pasqualin 2023년 6월 11일
댓글: Paris Pasqualin 2023년 6월 11일
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