Make num2str in engineering format

103 ビュー (過去 30 日間)
Yoav Romach
Yoav Romach 2014 年 3 月 6 日
回答済み: Steven Lord 2021 年 11 月 22 日
Hey, I'm writing this code:
disp(['Cutoff frequency: ',num2str(wcutoff,'%e')]);
and this displays the frequency in an normal exponential format, but I want it to be in eng format. This means that instead of:
I want it to show:
Is that possible? A possible workaround could be to use "format shortEng" and than separate the display code into 2 lines, but than I need to find a way that it'll display it in one output line or something like that...


Yoav Romach
Yoav Romach 2014 年 3 月 7 日
編集済み: Yoav Romach 2014 年 3 月 7 日
Ok, I've actually came up with a better, simpler solution:
format shortEng
disp([' Cutoff frequency: ', strtrim(evalc('disp(wcutoff)'))]);
strtrim is needed because the evalc returns numerous leading and trailing white space, but this produces exactly the output I wanted, without the helper function.
  4 件のコメント
Star Strider
Star Strider 2014 年 3 月 7 日
My pleasure!


その他の回答 (3 件)

Steven Lord
Steven Lord 2021 年 11 月 22 日
If you're using release R2021a or later I would use formattedDisplayText instead of evalc.
wcutoff = 5.2e10;
t = formattedDisplayText(wcutoff, 'NumericFormat', 'shortEng')
t =
" 52.0000e+009 "

Harry Dymond
Harry Dymond 2019 年 7 月 16 日
For the benifit of those stumbling across this old thread in search of answers: my FEX submission num2eng will do what the OP is asking for.

Jos (10584)
Jos (10584) 2014 年 3 月 7 日
A similar effect can be obtained by manipulating the string rather than the numbers
X = 52346574 ;
s1 = sprintf('%.2e',X)
v = sscanf(s1,'%d.%de%d')
s2 = sprintf('%d%.1fe%02d',v(1), v(2)/10, v(3)-1)
disp(['Cutoff frequency: ', s2]);
Which can be made into a function handle:
Val2EngString = @(X) sprintf('%d%.1fe%02d',[1 .1 1] .* sscanf(sprintf('%.2e',X),'%d.%de%d').' - [0 0 1])
  1 件のコメント
Star Strider
Star Strider 2014 年 3 月 7 日
I never considered that approach. I’ll take a look at it.



Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by