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
solx = '0.0001235'
soly
soly = '1.235E-05'
Is there any way that I can get solx to be formatted like soly?

 採用された回答

David Hill
David Hill 2021 年 8 月 17 日

0 投票

soly=num2str(y,'%.3E')
solx=num2str(x,'%.3E')

3 件のコメント

Noam Ouzana
Noam Ouzana 2021 年 8 月 18 日
thanks! but I want the scientific formatting to show only when needed, i.e only when the number has more than four figures, since it's won't always be the case.
I assume that the reason it won't work for the given x value is because the formatting won't really reduce the amount of characters, but I'd still prefer it that way.
Simon Chan
Simon Chan 2021 年 8 月 18 日
Write a function which can change the display format based on the magnitude of the data.
Walter Roberson
Walter Roberson 2021 年 8 月 18 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で File Operations についてさらに検索

製品

リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by