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

Réponses (3)

a=[36.897, 1369.4587
0.3, 2.5
1, 2
25.8,3.125];
b=num2str(a);
b(:,8:12)=[]

1 commentaire

Thanks.
But is there a way to specify that in sprintf? '%10.?f' ?
What should be the '?' then?

Connectez-vous pour commenter.

sprintf('%20.2f ', a)

3 commentaires

Thanks, but I can not lock the digits after the dot. In the same column, I could have values such as 2.5, 1369.4587, or 2. We really want to conserve the original significant number for each of these individual values.
I guess it should be: sprintf('%20f ', a)
Thanks.
You might want to use %20g perhaps.

Connectez-vous pour commenter.

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.

Tags

Question posée :

le 26 Avr 2011

Community Treasure Hunt

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

Start Hunting!

Translated by