mat2str

Converts a matrix to text that evaluates back to the matrix.

Category: Language Fundamentals > Data Types

Syntax

s = mat2str(A)

Returns a character vector such as '[1 2;3 4]'.

s = mat2str(A, n)

Uses n significant digits for each number (15 by default).

Description

Columns are separated by spaces and rows by semicolons. Logical values appear as true and false, complex numbers as 1+2i, and integer classes are wrapped in their constructor, as in int8([1 2]). A char vector is returned in quotes.

With the default 15 digits the text reproduces ordinary doubles; a smaller n gives a shorter, rounded form.

Input arguments

A
Numeric, logical or char matrix (two dimensions at most).
n
Number of significant digits.

Output arguments

s
Character vector. Passing it to eval rebuilds A.

Example

Matrices as text

mat2str([1 2; 3 4.5])
mat2str([true false])
mat2str(pi, 4)
mat2str(int8([1 2]))
Output
ans = '[1 2;3 4.5]'
ans = '[true false]'
ans = '3.142'
ans = 'int8([1 2])'

Notes

For a scalar single input MATLAB wraps the text in single(...); MatLivre prints the bare number. A single array is wrapped in single(...).

See also

num2str sprintf disp eval str2num