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:
5.2e10
I want it to show:
52.0e9
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일

0 개 추천

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

Yoav Romach
Yoav Romach 2014년 3월 7일
I know it's a bit weird choosing my own answer, but is it possible? Could be useful for other users to see this solution!
Star Strider
Star Strider 2014년 3월 7일
They'll see it anyway.
I came up with my function because I got the impression from your original post that you’d already tried the format shortEng idea and it didn’t produce the result you wanted.
If you want to revert to whatever you had before you invoked format shortEng, (perhaps format shortE), you have to follow the statements you posted with format shortE. It will stay format shortEng until you change it. With the function I wrote, you don't have to change it at all. Just use sprintf with its output.
You can accept your own Answer, but you don’t get any Reputation Points for it. You cannot vote for your own answer. You can’t un-accept an answer you have already accepted.
Yoav Romach
Yoav Romach 2014년 3월 7일
Haa, I see, Don't care about the credit though.
My original problem with "format shortEng" was that it didn't work :) I had to push another "evalc('disp(wcutoff)')" inside, and I only thought of that today.
Thanks a lot anyway!
Star Strider
Star Strider 2014년 3월 7일
My pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Steven Lord
Steven Lord 2021년 11월 22일

3 개 추천

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일

1 개 추천

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일

0 개 추천

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])
disp(Val2EngString(2340000000))

댓글 수: 1

Star Strider
Star Strider 2014년 3월 7일
I never considered that approach. I’ll take a look at it.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

질문:

2014년 3월 6일

답변:

2021년 11월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by