Page 90 - Basics of MATLAB and Beyond
P. 90

>> v = sin((1:3)*pi/6)
                               v  =
                                   0.5000     0.8660    1.0000
                               >> num2str(v)
                               ans  =
                               0.5    0.866031
                               >> q = reshape(1:9,3,3)
                               q  =
                                    1     4      7
                                    2     5      8
                                    3     6      9

                               >> int2str(q)
                               ans  =
                               1  4  7
                               2  5  8
                               3  6  9
                               >> size(ans)
                               ans  =
                                    3     7
                               You can tell num2str how many digits to display by giving it a second
                               parameter:
                               >> num2str(pi,2)
                               ans  =
                               3.1
                               >> num2str(pi,15)
                               ans  =
                               3.14159265358979
                               The second parameter of num2str can also specify the format by means
                               of C language conversions. These involve the percent character, width
                               and precision fields, and conversion characters: d, f, e, etc. (see table
                               below). The basic idea is to use a string of characters beginning with %
                               to control the formatting. For example, to output five decimal places in
                               a field of 12 characters with exponential notation, use:

                               >> num2str(pi,’%12.5e’)
                               ans  =
                               3.14159e+00

                               >> num2str(-pi,’%12.5e’)
                               ans  =
                               -3.14159e+00

                               >> num2str(pi*1e100,’%12.5e’)



                               c   2000 by CRC Press LLC
   85   86   87   88   89   90   91   92   93   94   95