Documentation Center

  • Trial Software
  • Product Updates


Convert number to string




str = num2str(A) converts array A into a string representation str. The output format depends upon the magnitude of the original values and sometimes includes exponents. num2str is useful for labeling and titling plots with numeric values.


str = num2str(A,precision) returns a string representation with the maximum number of digits specified by precision.


str = num2str(A,formatSpec) applies a format specified by formatSpec to all elements of A.


expand all

Default Conversions of Floating-Point Values

Convert pi and eps to text strings.

str = num2str(pi)
str =


str = num2str(eps)
str =


Specifying Precision

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

A = gallery('normaldata',[2,2],0);
precision = 3;
str = num2str(A,precision)
str =

-0.433     0.125
 -1.67     0.288

Specifying Formatting

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

A = gallery('uniformdata',[2,3],0) * 9999;
formatSpec = '%10.5e\n';
str = num2str(A,formatSpec)
str =


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

Input Arguments

expand all

A — Input arrayscalar | vector | matrix | multidimensional array

Input array, specified as a scalar, vector, matrix, or multidimensional array of real or complex numbers.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

precision — Numeric precisionpositive integer

Numeric precision of the output string, specified as a positive integer. This is the maximum number of digits in the output string.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

formatSpec — Format of the output fieldsstring

Format of the output fields, specified as a string.

The string can include a percent sign followed by a conversion character. The following table lists the available conversion characters and subtypes.

Value TypeConversionDetails

Integer, signed

%d or %i

Base 10

Integer, unsigned


Base 10


Base 8 (octal)


Base 16 (hexadecimal), lowercase letters af


Same as %x, uppercase letters AF

Floating-point number


Fixed-point notation


Exponential notation, such as 3.141593e+00


Same as %e, but uppercase, such as 3.141593E+00


The more compact of %e or %f, with no trailing zeros


The more compact of %E or %f, with no trailing zeros

%bx or %bX

Double-precision hexadecimal, octal, or decimal value
Example: %bx prints pi as 400921fb54442d18

%tx or %tX

Single-precision hexadecimal, octal, or decimal value
Example: %tx prints pi as 40490fdb



Single character


String of characters

The string can include optional operators, which appear in the following order (includes spaces for clarity):

Optional operators include:

  • Identifier

    Order for processing inputs. Use the syntax n$, where n represents the position of the value in the input list.

    For example, '%3$s %2$s %1$s %2$s' prints inputs 'A', 'B', 'C' as follows: C B A B.

  • Flags


    Left-justify. Example: %-5.2f


    Print sign character (+) for positive values. Example: %+5.2f

    ' '

    Pad to field width with spaces before the value. Example: % 5.2f


    Pad to field width with zeros. Example: %05.2f


    Modify selected numeric conversions:

    • For %o, %x, or %X, print 0, 0x, or 0X prefix.

    • For %f, %e, or %E, print decimal point even when precision is 0.

    • For %g or %G, do not remove trailing zeros or decimal point.

    Example: %#5.0f

  • Field width

    Minimum number of characters to print. Can be a number, or an asterisk (*) to refer to an argument in the input list. For example, the input list ('%12d', intmax) is equivalent to ('%*d', 12, intmax).

  • Precision

    For %f, %e, or %E:

    Number of digits to the right of the decimal point.
    Example: '%6.4f' prints pi as '3.1416'

    For %g or %G

    Number of significant digits.
    Example: '%6.4g' prints pi as ' 3.142'

    Can be a number, or an asterisk (*) to refer to an argument in the input list. For example, the input list ('%6.4f', pi) is equivalent to ('%*.*f', 6, 4, pi).

The string can also include combinations of the following:

  • Literal text to print. To print a single quotation mark, include '' in formatSpec.

  • Control characters, including:


    Percent character








    Form feed


    New line


    Carriage return


    Horizontal tab


    Vertical tab


    Character whose ASCII code is the hexadecimal number, N


    Character whose ASCII code is the octal number, N

The following limitations apply to conversions:

  • Numeric conversions print only the real component of complex numbers.

  • If you specify a conversion that does not fit the data, such as a string conversion for a numeric value, MATLAB® overrides the specified conversion, and uses %e.

  • If you apply a string conversion (%s) to integer values, MATLAB converts values that correspond to valid character codes to characters. For example, '%s' converts [65 66 67] to ABC.

Output Arguments

expand all

str — String representation of input arraycharacter array

String representation of the input array, A, returned as a character array.

More About

expand all


num2str trims any leading spaces from a string, even when formatSpec includes a space character flag. For example, num2str(42.67,'% 10.2f') returns a 1-by-5 character array '42.67'.

See Also

| | | |

Was this topic helpful?