Stop exponential answers

조회 수: 501 (최근 30일)
Philip
Philip 2011년 6월 7일
댓글: Vishal Singh 2021년 2월 26일
Dear all,
I have two values representing the max and min of a data vector.
max_val = 0.9855;
min_val = 0.9851;
Both are of type 'double'. However, when I try to subtract the min from the max:
diff = max_val - min_val;
the answer 3.6023e-04 is returned instead of 0.0004. How can I ensure that this answer is expressed without the exponentiation?
  댓글 수: 1
Vishal Singh
Vishal Singh 2021년 2월 26일
you can use "format short g" command in the start of the code.I am a fresher in matlab but as far as i know it can help to get rid of e in the answer.

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

채택된 답변

Titus Edelhofer
Titus Edelhofer 2011년 6월 7일
Hi,
there is no type in format that generally tells MATLAB to use such a format. Nevertheless you can always use sprintf to make a string in the format you like, in this case it would be
sprintf('%f', diff)
ans =
0.000400
Titus
  댓글 수: 3
Guillaume
Guillaume 2016년 8월 11일
format does not alter the result either. It only alters the way it is displayed.
And of course, it is much simpler to change the format to short/long g and display the number as usual than having to call sprintf all the time.
John D'Errico
John D'Errico 2016년 8월 11일
I wish we could upvote comments. This one by Guillaume is correct of course. (The answer by Titus is also correct.) But the point is, people seem to think that use of the format command actually changes the number internally. That does not happen. Format only affects the output as it appears in the command window.

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

추가 답변 (2개)

John D'Errico
John D'Errico 2011년 6월 7일
I tend to work normally with the display format set as
format short g
for most work. This gives me numbers in a format that I like as often as possible, only going into scientific notation when necessary. Thus
>> format short g
>> X = 3.6023e-04
X =
0.00036023
"short g" is more compact than the alternative of "long g", and most of the time I don't need to see 15 decimal digits in my results.

Robert Cumming
Robert Cumming 2011년 6월 7일
to make it write the number you could do:
format long g
to actually round your number to the correct number of decimal places you could make an inline function, e.g.:
dcp = inline ( 'round(input.*10.^number)./10.^number' )
dcp(diff,4) % this will round to 4 decimal places
p.s. its not an exponential its engineering format that your answer is expressed as.
  댓글 수: 2
John D'Errico
John D'Errico 2011년 6월 7일
Note that inline functions are not at all efficient. function handles are far better choices.
dcp = @(inp,number) round(inp.*10.^number)./10.^number;
It is also a poor choice to define a variable with the name input, as this overloads the existing matlab function input.m, preventing you from later using that tool.
Robert Cumming
Robert Cumming 2011년 6월 7일
it was just a simple example to show what could be done. My actual preference is to have a seperate function which is saved on my path so its always available to me and my team.
As far as using input - I get your point but in simple small functions I dont think its an issue.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by