How can we let Matlab to choose the decimal place?

Hi there, I would like to ask if like my answer is 1.11111 and my error is 0.001234, so suppose I should write my answer like 1.111+-0.001. So is there any output method so that MATLAB can know that it should display the first non-zero digit for the error and the answer should have the same decimal place as the error?

 Réponse acceptée

Something like this?
Value=pi;
Err=0.00356;
Str=sprintf('%1.0e',Err);
Err=str2double(Str)
D=str2double(Str(3:end))
Value=round(Value/10^D)*10^D
FormatStr=sprintf('%%15.%df',abs(D));
ValueStr=sprintf(FormatStr,Value)
ErrStr=sprintf(FormatStr,Err)

Plus de réponses (1)

Walter Roberson
Walter Roberson le 24 Nov 2011

0 votes

No, there is no MATLAB output method that does that. You would have to program one.
Examine the behavior of the floor() and ceil() of log10(error); you will probably find that you need a minimum of 1 "if" condition, perhaps even 2. Watch out for values above or below 1, and watch out for exact powers of 10 -- e.g., 99 vs 100 vs 101 and .099 vs .100 vs .101 .

Community Treasure Hunt

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

Start Hunting!

Translated by