Stop exponential answers
502 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Philip
il 7 Giu 2011
Commentato: Vishal Singh
il 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 Commento
Vishal Singh
il 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.
Risposta accettata
Titus Edelhofer
il 7 Giu 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 Commenti
Guillaume
il 11 Ago 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
il 11 Ago 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.
Più risposte (2)
John D'Errico
il 7 Giu 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.
0 Commenti
Robert Cumming
il 7 Giu 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 Commenti
John D'Errico
il 7 Giu 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
il 7 Giu 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.
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!