필터 지우기
필터 지우기

How to add spaces in front of numbers to make their length a constant value

조회 수: 10 (최근 30일)
Liqing
Liqing 2011년 4월 26일
For example:
Thank you.

답변 (3개)

Fangjun Jiang
Fangjun Jiang 2011년 4월 26일
a=[36.897, 1369.4587
0.3, 2.5
1, 2
25.8,3.125];
b=num2str(a);
b(:,8:12)=[]
  댓글 수: 1
Liqing
Liqing 2011년 4월 26일
Thanks.
But is there a way to specify that in sprintf? '%10.?f' ?
What should be the '?' then?

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2011년 4월 26일
sprintf('%20.2f ', a)
  댓글 수: 3
Liqing
Liqing 2011년 4월 26일
I guess it should be: sprintf('%20f ', a)
Thanks.
Walter Roberson
Walter Roberson 2011년 4월 26일
You might want to use %20g perhaps.

댓글을 달려면 로그인하십시오.


Fangjun Jiang
Fangjun Jiang 2011년 4월 26일
a=[36.897, 1369.4587
0.3, 2.5
1, 2
25.8,3.125];
b=sprintf('%6g%10.8g\n',a')
You might need to adjust the field width and precision based on your larger set of data.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by