MATLAB num2str: Preserving Valid Digits

  • MATLAB
  • Thread starter shwathshav
  • Start date
  • Tags
    Matlab
In summary, the topic MATLAB num2str is closed, therefore I am posting this as a reply to that thread. The suggestion by marcusl won't work for numbers of various format, but same number of valid digits.f
  • #1
Dear Users,

the topic MATLAB num2str is closed, therefore I am posting this as a reply to that thread.

The suggestion by marcusl won't work for numbers of various format, but same number of valid digits. Example: The numbers

0.0056
0.0060
0.043
0.050
0.32
0.40

are valid to their last two digits. After converting by num2str(), only three of them will have that precision. The other three will have only one valid digit.

My main goal is to print these values into a plot without loosing information (the number of valid digits).

Does anybody have any suggestion, please?

Cheers,
Milan
 
  • #2
change the format string to get the number of digits you want or specify the number of digits via the precision, eg num2str(x,4) should handle all of your cases.
 
  • #3
No, it does not handle

Code:
for i=1:4
num2str([0.0056, 0.0060, 0.043, 0.050, 0.32, 0.40],i)
end

ans =

0.006   0.006    0.04    0.05     0.3     0.4


ans =

0.0056    0.006    0.043     0.05     0.32      0.4


ans =

0.0056     0.006     0.043      0.05      0.32       0.4


ans =

0.0056      0.006      0.043       0.05       0.32        0.4

The number of spaces is increasing thou. But how can I use it? Anyway, for a single number there is no difference:

Code:
>> for i=1:4
num2str(0.00560,i)
end

ans =

0.006


ans =

0.0056


ans =

0.0056


ans =

0.0056

But yeah, it is also documented in the MATLAB help for num2str:
str = num2str(A, precision) converts the array A into a string representation str with the maximum number of digits specified by precision
 
Last edited:
  • #4
It's not failing. It is doing what you asked, just not the way you want. What you apparently want are trailing zeros. num2str(A,precision) doesn't do that. If you insist on having those trailing zeros, you'll have to use the formatted version of num2str, e.g. num2str(A,"%.1e ") .

Aside:
The following probably result in what one legitimately could call "failing":

num2str([1.105, 1.115, 1.125, 1.135, 1.145, 1.155, 1.165, 1.175, 1.185, 1.195],2)

If your computer is anything like mine, the rounding will not be consistent.
 
  • #5
I expected a failing around 1.15, but there is nothing surprising, IMO.

A part of your idea:
Code:
num2str([1.145, 1.155],2)

ans =

1.1      1.2
IMO, it is OK.

A new failing example:
Code:
num2str([1.145, 1.150, 1.155],2)

ans =

1.1      1.1      1.2
<=> rounding 1.15 to the first decimal place should be 1.2, shouldn't it?

Cheers, M
 
  • #6
num2str([1.105, 1.115, 1.125, 1.135, 1.145, 1.155, 1.165, 1.175, 1.185, 1.195],2)

Oops. That should have been a precision of 3, not 2. Try

num2str([1.105, 1.115, 1.125, 1.135, 1.145, 1.155, 1.165, 1.175, 1.185, 1.195],3)

rounding 1.15 to the first decimal place should be 1.2, shouldn't it?
It's the same problem as the one I was trying to illustrate. How C (and hence Matlab) handles those corner cases is a bit suspect.
 
  • #7
OK, confirmed: The the last number (1.195) shall be displayed as 1.20, otherwise a meticulous scientist looses information... :)
Code:
num2str([1.105, 1.115, 1.125, 1.135, 1.145, 1.155, 1.165, 1.175, 1.185, 1.195],3)

ans =

1.11      1.12      1.13      1.14      1.15      1.16      1.17      1.18      1.19       1.2
so a workaround to have trailing zeros would be then:
Code:
num2str([0.0156; 0.0060; 0.043; 0.050; 0.32; 0.40],'%.1e')

ans =

1.6e-002
6.0e-003
4.3e-002
5.0e-002
3.2e-001
4.0e-001
Thank you D H!
 

Suggested for: MATLAB num2str: Preserving Valid Digits

Replies
4
Views
499
Replies
4
Views
932
Replies
6
Views
742
Replies
5
Views
1K
Replies
2
Views
1K
Replies
32
Views
1K
Replies
2
Views
969
Replies
2
Views
1K
Replies
1
Views
958
Back
Top