num2str

Converts numbers to a character array.

Category: Language Fundamentals > Data Types

Syntax

s = num2str(A)

Converts using a precision chosen from the magnitude of the values (about five significant digits).

s = num2str(A, precision)

Uses the given number of significant digits.

s = num2str(A, formatSpec)

Formats each element with a sprintf-style format.

Description

Integers print without a decimal point. For non-integers the default keeps four digits after the leading digit, so pi becomes '3.1416'; the precision argument changes that, and formatSpec gives full control. Complex values print as 3+4i.

For a vector or matrix, the elements are laid out in aligned columns separated by two spaces. To combine numbers with other text, sprintf or string(x) are more flexible.

Input arguments

A
Numeric, logical or char array.
precision
Number of significant digits.
formatSpec
Format such as '%8.3f'.

Output arguments

s
Char array. A matrix input gives one row of text per row of A.

Example

num2str(pi)
num2str(pi, 8)
num2str([1 2 3])
num2str(3.14159, '%8.3f')
Output
ans = '3.1416'
ans = '3.1415927'
ans = '1  2  3'
ans = '   3.142'

See also

sprintf str2num str2double string mat2str disp