mat2str

Convert matrix to string

Syntax

str = mat2str(A)
str = mat2str(A,n)
str = mat2str(A, 'class')
str = mat2str(A, n, 'class')

Description

str = mat2str(A) converts matrix A into a string. This string is suitable for input to the eval function such that eval(str) produces the original matrix to within 15 digits of precision.

str = mat2str(A,n) converts matrix A using n digits of precision.

str = mat2str(A, 'class') creates a string with the name of the class of A included. This option ensures that the result of evaluating str will also contain the class information.

str = mat2str(A, n, 'class') uses n digits of precision and includes the class information.

Limitations

The mat2str function is intended to operate on scalar, vector, or rectangular array inputs only. An error will result if A is a multidimensional array.

Examples

Example 1

Consider the matrix

x = [3.85 2.91; 7.74 8.99]
x =
    3.8500    2.9100
    7.7400    8.9900

The statement

A = mat2str(x)

produces

A =
   [3.85 2.91;7.74 8.99]

where A is a string of 21 characters, including the square brackets, spaces, and a semicolon.

eval(mat2str(x)) reproduces x.

Example 2

Create a 1-by-6 matrix of signed 16-bit integers, and then use mat2str to convert the matrix to a 1-by-33 character array, A. Note that output string A includes the class name, int16:

x1 = int16([-300 407 213 418 32 -125]);

A = mat2str(x1, 'class')
A =
   int16([-300 407 213 418 32 -125])

class(A)
ans =
   char

Evaluating the string A gives you an output x2 that is the same as the original int16 matrix:

x2 = eval(A);

if isnumeric(x2) && isa(x2, 'int16') && all(x2 == x1)
   disp 'Conversion back to int16 worked'
end

Conversion back to int16 worked
Was this topic helpful?