num2str - Convert number to string

Syntax

str = num2str(A)
str = num2str(A, precision)
str = num2str(A, format)

Description

The num2str function converts numbers to their string representations. This function is useful for labeling and titling plots with numeric values.

str = num2str(A) converts array A into a string representation str with roughly four digits of precision and an exponent if required.

str = num2str(A, precision) converts the array A into a string representation str with maximum precision specified by precision. Argument precision specifies the number of digits the output string is to contain. The default is four.

str = num2str(A, format) converts array A using the supplied format. (See fprintf for format string details.) By default, num2str displays floating point values using '%11.4g' format (four significant digits in exponential or fixed-point notation, whichever is shorter).

If the input array is integer-valued, num2str returns the exact string representation of that integer. The term integer-valued includes large floating-point numbers that lose precision due to limitations of the hardware.

num2str removes any leading spaces from the output string. Thus, num2str(42.67, '%10.2f') returns a 1-by-5 character array '42.67'.

Examples

num2str(pi) is 3.142.

num2str(eps) is 2.22e-16.

num2str(randn(2,2),3) produces the following string matrix:

num2str(randn(2,2),3)
ans =
  0.538     -2.26
   1.83     0.862

num2str with a format of %10.5e\n returns a matrix of strings in exponential format, having 5 decimal places, with each element separated by a newline character:

x = rand(2,3) * 9999;            % Create a 2-by-3 matrix.

A = num2str(x, '%10.5e\n')       % Convert to string array.
A =
6.87255e+003
1.55597e+003
8.55890e+003
3.46077e+003
1.91097e+003
4.90201e+003

See Also

mat2str, int2str, str2num, sprintf, fprintf

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS