Azzera filtri
Azzera filtri

num2str default format

18 visualizzazioni (ultimi 30 giorni)
Ilya Gurin
Ilya Gurin il 4 Mag 2020
Commentato: Stephen23 il 4 Mag 2020
I want to print multiple values using fprintf, but with the same number formatting as num2str. Is there a format string that will give the same behavior as num2str?

Risposte (2)

dpb
dpb il 4 Mag 2020
'%.5g'
is default format iirc what num2str does and the way %g counts precision.
  1 Commento
Stephen23
Stephen23 il 4 Mag 2020
>> num = intmax('uint32');
>> num2str(num)
ans =
4294967295
>> sprintf('%.5g',num)
ans =
4.295e+09

Accedi per commentare.


Walter Roberson
Walter Roberson il 4 Mag 2020
no. num2str does a bunch of width calculations and chooses output format based on integer vs floating point
  1 Commento
Ilya Gurin
Ilya Gurin il 4 Mag 2020
Haha, I want to accept both your answers. sprintf(pi, '%.5g') and num2str(pi) are the same. If I scale the input up by 1E10 or 1E100, the results are different, but I think I like the behavior of '%.5g' better.

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Tag

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by