This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Convert matrix to characters


chr = mat2str(X)
chr = mat2str(X,n)
chr = mat2str(___,'class')



chr = mat2str(X) converts the numeric matrix X into a character vector that represents the matrix, with up to 15 digits of precision.

You can use chr as input to the eval function. For example, A = eval(chr) reproduces the values from the original matrix to the precision specified in chr.


chr = mat2str(X,n) converts X using n digits of precision.


chr = mat2str(___,'class') includes the name of the class, or data type, of X in chr. You can use this syntax with any of the arguments from the previous syntaxes.

If you use this syntax to produce chr, then A = eval(chr) also reproduces the data type of the original matrix.


collapse all

Convert a numeric matrix to a character vector.

chr = mat2str([3.85 2.91; 7.74 8.99])
chr = 
'[3.85 2.91;7.74 8.99]'

You can convert chr back to a numeric matrix using the eval function.

A = eval(chr)
A = 2×2

    3.8500    2.9100
    7.7400    8.9900

Convert a numeric matrix to a character vector, to three digits of precision.

chr = mat2str([3.1416 2.7183],3)
chr = 
'[3.14 2.72]'

Create an array of integers and convert it to a character vector. By default, the output of mat2str represents an array of doubles. To represent a different numeric type in the output, use the 'class' input argument.

Create a vector of 16-bit unsigned integers.

X = uint16([256 512])
X = 1x2 uint16 row vector

   256   512

Convert X to a character vector, including the data type of X.

chr = mat2str(X,'class')
chr = 
'uint16([256 512])'

Convert chr back to an array of integers. A has the same values and data type as X.

A = eval(chr)
A = 1x2 uint16 row vector

   256   512

Input Arguments

collapse all

Input array, specified as a numeric matrix.

Digits of precision, specified as a positive integer.


  • mat2str returns character arrays only. Starting in R2016b, you can convert numeric arrays to string arrays using the string function.

Extended Capabilities

Introduced before R2006a