Stop exponential answers

493 views (last 30 days)
Philip
Philip on 7 Jun 2011
Commented: Vishal Singh on 26 Feb 2021
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 Comment
Vishal Singh
Vishal Singh on 26 Feb 2021
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.

Sign in to comment.

Accepted Answer

Titus Edelhofer
Titus Edelhofer on 7 Jun 2011
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 Comments
Guillaume
Guillaume on 11 Aug 2016
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 on 11 Aug 2016
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.

Sign in to comment.

More Answers (2)

John D'Errico
John D'Errico on 7 Jun 2011
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 on 7 Jun 2011
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 Comments
John D'Errico
John D'Errico on 7 Jun 2011
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 on 7 Jun 2011
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!