뒤로: Concatenating Strings, 위로: Creating Strings [차례][찾아보기]
Apart from the string concatenation functions (see Concatenating Strings)
which cast numerical data to the corresponding UTF-8 encoded characters, there
are several functions that format numerical data as strings. mat2str
and num2str
convert real or complex matrices, while int2str
converts integer matrices. int2str
takes the real part of complex
values and round fractional values to integer. A more flexible way to format
numerical data as strings is the sprintf
function
(see Formatted Output, sprintf).
Format real, complex, and logical matrices as strings.
The returned string may be used to reconstruct the original matrix by using
the eval
function.
The precision of the values is given by n. If n is a scalar
then both real and imaginary parts of the matrix are printed to the same
precision. Otherwise n(1)
defines the precision of the real
part and n(2)
defines the precision of the imaginary part.
The default for n is 15.
If the argument "class"
is given then the class of x is
included in the string in such a way that eval
will result in the
construction of a matrix of the same class.
mat2str ([ -1/3 + i/7; 1/3 - i/7 ], [4 2]) ⇒ "[-0.3333+0.14i;0.3333-0.14i]" mat2str ([ -1/3 +i/7; 1/3 -i/7 ], [4 2]) ⇒ "[-0.3333+0i 0+0.14i;0.3333+0i -0-0.14i]" mat2str (int16 ([1 -1]), "class") ⇒ "int16([1 -1])" mat2str (logical (eye (2))) ⇒ "[true false;false true]" isequal (x, eval (mat2str (x))) ⇒ 1
Convert a number (or array) to a string (or a character array).
The optional second argument may either give the number of significant
digits (precision) to be used in the output or a format template
string (format) as in sprintf
(see Formatted Output).
num2str
can also process complex numbers.
Examples:
num2str (123.456) ⇒ 123.456 num2str (123.456, 4) ⇒ 123.5 s = num2str ([1, 1.34; 3, 3.56], "%5.1f") ⇒ s = 1.0 1.3 3.0 3.6 whos s ⇒ Variables in the current scope: Attr Name Size Bytes Class ==== ==== ==== ===== ===== s 2x8 16 char Total is 16 elements using 16 bytes num2str (1.234 + 27.3i) ⇒ 1.234+27.3i
The num2str
function is not very flexible. For better control
over the results, use sprintf
(see Formatted Output).
Programming Notes:
For MATLAB compatibility, leading spaces are stripped before returning the string.
Integers larger than flintmax
may not be displayed correctly.
For complex x, the format string may only contain one output conversion specification and nothing else. Otherwise, results will be unpredictable.
Any optional format specified by the programmer is used without modification. This is in contrast to MATLAB which tampers with the format based on internal heuristics.
Convert an integer (or array of integers) to a string (or a character array).
int2str (123) ⇒ 123 s = int2str ([1, 2, 3; 4, 5, 6]) ⇒ s = 1 2 3 4 5 6 whos s ⇒ Variables in the current scope: Attr Name Size Bytes Class ==== ==== ==== ===== ===== s 2x7 14 char Total is 14 elements using 14 bytes
This function is not very flexible. For better control over the
results, use sprintf
(see Formatted Output).
Programming Notes:
Non-integers are rounded to integers before display. Only the real part of complex numbers is displayed.
뒤로: Concatenating Strings, 위로: Creating Strings [차례][찾아보기]