Why does NUM2STR not return the correct string format that I specified?

조회 수: 9 (최근 30일)
When I use NUM2STR in the following example, the result is a character array of length 6 instead of 10:
d = num2str(123.456, '%10.2f');
whos d
Name Size Bytes Class
d 1x6 12 char array
Grand total is 6 elements using 12 bytes

채택된 답변

MathWorks Support Team
MathWorks Support Team 2009년 6월 27일
This is the expected behavior of NUM2STR. NUM2STR removes leading spaces from variables.
As a workaround, you can utilize the SPRINTF command, as shown in the following example:
d = sprintf('%10.2f', 123.456);
The result of the variable d will now be a character array whose length is 10 elements.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by