Convert matrix to character vector
str = mat2str(A)
str = mat2str(A,n)
str = mat2str(A, 'class
')
str = mat2str(A, n, 'class
')
str = mat2str(A)
converts
matrix A
into a character vector. The character
vector 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, '
creates
a character vector with the name of the class of class
')A
included.
This option ensures that the result of evaluating str
will
also contain the class information.
str = mat2str(A, n, '
uses class
')n
digits
of precision and includes the class information.
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.
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 character vector of 21 characters,
including the square brackets, spaces, and a semicolon.
eval(mat2str(x))
reproduces x
.
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 vector, A
.
Note that 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 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