Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB num2str 2

  1. Sep 24, 2012 #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. jcsd
  3. Sep 24, 2012 #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.
     
  4. Sep 26, 2012 #3
    No, it does not handle

    Code (Text):

    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 (Text):

    >> 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:
     
    Last edited: Sep 26, 2012
  5. Sep 26, 2012 #4

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
     
  6. Sep 26, 2012 #5
    I expected a failing around 1.15, but there is nothing surprising, IMO.

    A part of your idea:
    Code (Text):

    num2str([1.145, 1.155],2)

    ans =

    1.1      1.2
     
    IMO, it is OK.

    A new failing example:
    Code (Text):

    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
     
  7. Sep 26, 2012 #6

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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)

    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.
     
  8. Sep 27, 2012 #7
    OK, confirmed: The the last number (1.195) shall be displayed as 1.20, otherwise a meticulous scientist looses information... :)
    Code (Text):
    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 (Text):
    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!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: MATLAB num2str 2
  1. MATLAB num2str (Replies: 1)

Loading...