using fprintf to print same length values

8 visualizaciones (últimos 30 días)
Ronaldo
Ronaldo el 30 de Dic. de 2013
Editada: Daniel el 1 de Sept. de 2017
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

Respuestas (2)

Azzi Abdelmalek
Azzi Abdelmalek el 30 de Dic. de 2013
Editada: Azzi Abdelmalek el 30 de Dic. de 2013
b=360.000305
m=9 % fixed length
out=sprintf('%9.9f',b)
ii=numel(strfind(out,'.'))
out=out(1:m+ii)
  8 comentarios
Ronaldo
Ronaldo el 31 de Dic. de 2013
Please add the following line (for the sake of completeness). I will accept the answer. Thanks for your great help.
fprintf('%s\n', out);
Azzi Abdelmalek
Azzi Abdelmalek el 31 de Dic. de 2013
Ronaldo, I am not adding anything.

Iniciar sesión para comentar.


Daniel
Daniel el 1 de Sept. de 2017
Editada: Daniel el 1 de Sept. de 2017
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)

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by