How can I properly format small numbers using num2str?
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Noam Ouzana
am 17 Aug. 2021
Kommentiert: Walter Roberson
am 18 Aug. 2021
I was assigned to work on the aesthetics of a table that shows collected data as string.
I want to show the numbers with 4 digits percision, but whenever the number is bigger the 10e-5, the output will include all the zeros, and only the four digits, e.g:
x=0.000123456;
y=0.0000123456;
solx=num2str(x,'%.4G');
soly=num2str(y,'%.4G');
solx
soly
Is there any way that I can get solx to be formatted like soly?
0 Kommentare
Akzeptierte Antwort
David Hill
am 17 Aug. 2021
soly=num2str(y,'%.3E')
solx=num2str(x,'%.3E')
3 Kommentare
Simon Chan
am 18 Aug. 2021
Write a function which can change the display format based on the magnitude of the data.
Walter Roberson
am 18 Aug. 2021
Simon is correct.
The only conditional formatting that MATLAB supplies in any released version, is what is done automatically by "format short" or "format long", or done by a %g format for sprintf() or fprintf() or compose() -- none of which permit tuning the boundaries of when values are to be shown in which varient.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!