using fprintf to print same length values
이전 댓글 표시
How can I use fprintf to print values with the same length (the number of digits after the pointer is changing based on the number of digits before the pointer)?
0.00000000
30.0000248
360.000305
답변 (2개)
Azzi Abdelmalek
2013년 12월 30일
편집: Azzi Abdelmalek
2013년 12월 30일
b=360.000305
m=9 % fixed length
out=sprintf('%9.9f',b)
ii=numel(strfind(out,'.'))
out=out(1:m+ii)
댓글 수: 8
Image Analyst
2013년 12월 30일
What is a?
Image Analyst
2013년 12월 30일
Never mind. I figured it out and corrected it:
b=[0.00000000
30.0000248
360.000305
12345.678]
m=9; % fixed length
for k = 1 : length(b)
out=sprintf('%9.9f',b(k));
ii=numel(strfind(out,'.'));
out=out(1:m+ii);
fprintf('%s\n', out);
end
Nice clever solution.
Ronaldo
2013년 12월 30일
Azzi Abdelmalek
2013년 12월 30일
Ronaldo, Analyst has corrected the answer, in my answer a should be out. Look at edited answer
Ronaldo
2013년 12월 30일
편집: Azzi Abdelmalek
2013년 12월 30일
Azzi Abdelmalek
2013년 12월 30일
This works fine, what is the problem?
Ronaldo
2013년 12월 31일
Azzi Abdelmalek
2013년 12월 31일
Ronaldo, I am not adding anything.
b=360.000305
m=9 % fixed length
out=sprintf('%9.9f',b)
ii=numel(strfind(out,'.'))
out=out(1:m+ii)
% Azzi's got no time for correctness
fprintf('%s\n', out)
카테고리
도움말 센터 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!