Display number with engineering notation

조회 수: 170 (최근 30일)
Jared
Jared 2011년 11월 8일
편집: Harry Dymond 2020년 5월 31일
I'm trying to display a number I read in with textscan in engineering notation. I was trying to switch the format to short eng and do:
evalc('disp(x)')
But it doesn't matter what format I set, the format textscan reads it in is what it uses.
  댓글 수: 1
Harry Dymond
Harry Dymond 2020년 5월 31일
편집: Harry Dymond 2020년 5월 31일
For the benifit of those stumbling across this old thread in search of answers: please see my answer below

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

채택된 답변

Walter Roberson
Walter Roberson 2011년 11월 8일
My tests disagree with Jared's findings:
>> result = textscan('133423.3e7','%f')
result =
[1334233000000]
>> format short eng
>> evalc('disp(result{1})')
ans =
1.3342e+012
>> result{1}
ans =
1.3342e+012
Engineering format was indeed used for the output, and the output is not the same as the format of the value that was read in nor of the format the data was stored in
  댓글 수: 2
Jared
Jared 2011년 11월 9일
During more experimentation, it works for an example like the one you posted. However, and I guess I forgot to put it in my original question, I am actually trying to set the string in a GUI using
set(handles.example,'String','example string'). For some reason, it does not seem to follow these formatting inputs.
Walter Roberson
Walter Roberson 2011년 11월 9일
Trying to set as input or as output? Are you using
textscan( get(handles.example,'String'), 'Some Format Here')
or are you using
set(handles.example, 'String', x) %where x is the numeric value
or are you using something like:
set(handles.example, 'String', evalc('format short eng; disp(x)'))
If you are trying to set the String parameter to a numeric value, then the conversion that MATLAB uses is not documented (as best I recall.)

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

추가 답변 (2개)

Harry Dymond
Harry Dymond 2020년 5월 31일
For the benifit of those stumbling across this old thread in search of answers: my FEX submission num2eng will do this, and offers SI prefixes as well as engineering notation.

owr
owr 2011년 11월 8일
Try sprintf with a format string. It converts the number to a text string with a format of your choosing. Some built in MATLAB functions use this trick, for example, edit the code for dlmread.
  댓글 수: 1
Jared
Jared 2011년 11월 8일
As far as I know, sprintf does not have a engineering notation format string.

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by