How can I transform a number to string with space using num2str?
조회 수: 62 (최근 30일)
이전 댓글 표시
What is the result of "num2str(1,'%2d')"? I think it should be " 1", but the results is "1". I mean there is no space before "1". Actually, the result of "num2str(1,'%02d')" is "01", and the result of "num2str(1,'%-2d')" is "1". And, the result of "sprintf('%2d',1)" is " 1".
How can I transform a number to string with space using num2str?
댓글 수: 0
채택된 답변
Simon
2013년 12월 9일
Hi!
Maybe this way?
num = [1000; 1001];
% numeric part
numpart = arrayfun(@(x) sprintf('%2d', mod(x,100)), num, 'UniformOutput', false);
% letter part
letterpart = char(mod(num,26*100)./100+65);
% concat everything
retsult = strcat(letterpart, numpart)
추가 답변 (2개)
Walter Roberson
2013년 12월 5일
Use sprintf instead of nu2str()
sprintf('%2d', 1)
댓글 수: 3
Simon
2013년 12월 5일
Hi!
Please compare
sprintf('%2d\n',[1;11])
and
sprintf('%d\n',[1;11])
Walter Roberson
2013년 12월 9일
numt = num(:).'; %make sure it is row vector
rem100 = mod(numt, 100);
lead2600 = mod((numt - rem100) ./ 100, 26) + 65;
longstr = sprintf('%c%2d\n', [lead2600; rem100]);
strs = regexp(longstr, '\n', 'split');
Jung-Woo
2013년 12월 9일
댓글 수: 1
Walter Roberson
2013년 12월 9일
You cannot use num2str() for this purpose. In situations in which every entry ends up with a leading blank, num2str() will strip off the leading blank.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!