Skip to Main Content Skip to Search
Product Documentation

num2str - Convert number to string

Syntax

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

Description

str = num2str(A) converts array A into a string representation str. When A contains floating-point numbers, the output format depends upon the size of the original values and sometimes includes exponents.

str = num2str(A,precision) converts the array A with the maximum number of digits specified by precision.

str = num2str(A,formatSpec) converts array A using the supplied formatSpec, as described on the fprintf function reference page.

Tips

Examples

Default Conversions of Floating-Point Values

Convert pi and eps to text strings.

str = num2str(pi)
str =
3.1416
str = num2str(eps)
str =
2.2204e-16

Specifying Precision

Specify the number of significant digits for floating-point values.

If you run this example, your values of A might differ.

A = randn(2,2);
precision = 3;
str = num2str(A,precision)
str =
  0.538     -2.26
   1.83     0.862

fprintf-Style Formats

Specify the width, precision, and other formatting for an array of floating-point values.

If you run this example, your values of A might differ.

A = rand(2,3) * 9999;
formatSpec = '%10.5e\n';
str = num2str(A,formatSpec)
str =
6.87255e+03
1.55597e+03
8.55890e+03
3.46077e+03
1.91097e+03
4.90201e+03

The format '%10.5e' prints each value in exponential format with five decimal places, and '\n' prints a new line character.

See Also

cast | fprintf | int2str | mat2str | sprintf | str2num

  


» Learn more
» Download free kit
» Get trial software

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