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.
  • #1
shwathshav
4
0
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
 
Physics news on Phys.org
  • #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.4ans =

0.0056    0.006    0.043     0.05     0.32      0.4ans =

0.0056     0.006     0.043      0.05      0.32       0.4ans =

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.006ans =

0.0056ans =

0.0056ans =

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
D H said:
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)

shwathshav said:
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!
 

FAQ: MATLAB num2str: Preserving Valid Digits

What is the purpose of the "num2str" function in MATLAB?

The "num2str" function in MATLAB is used to convert a numerical value into a string. This allows for easier manipulation and display of numerical data in a human-readable format.

What is the syntax for using the "num2str" function in MATLAB?

The basic syntax for the "num2str" function in MATLAB is:

num2str(value, precision)

The "value" parameter represents the numerical value to be converted, while the "precision" parameter specifies the number of digits after the decimal point to be included in the string.

Can the "num2str" function preserve valid digits when converting a number to a string?

Yes, the "num2str" function has the ability to preserve valid digits when converting a number to a string. This means that any trailing zeros after the decimal point will be included in the resulting string.

What happens if the "precision" parameter is not specified in the "num2str" function?

If the "precision" parameter is not specified, the "num2str" function will convert the numerical value to a string with a default precision of 15 digits after the decimal point.

Are there any limitations to the "num2str" function in terms of the size of the numerical value?

Yes, the "num2str" function has a limitation on the size of the numerical value it can handle. The maximum size is determined by the "realmax" function in MATLAB, which is dependent on the operating system and hardware used.

Back
Top