octave: Converting Numerical Data to Strings
5.3.2 Converting Numerical Data to Strings
------------------------------------------
Strings::) which cast numerical data to the corresponding ASCII
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 (Formatted Output, sprintf XREFsprintf.).
-- : S = mat2str (X, N)
-- : S = mat2str (X, N, "class")
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
See also: sprintf XREFsprintf, num2str XREFnum2str,
int2str XREFint2str.
-- : num2str (X)
-- : num2str (X, PRECISION)
-- : num2str (X, FORMAT)
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’ (Formatted
Output). ‘num2str’ can also process complex numbers.
Examples:
num2str (123.456)
⇒ "123.46"
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
⇒
Attr Name Size Bytes Class
==== ==== ==== ===== =====
s 2x8 16 char
num2str (1.234 + 27.3i)
⇒ "1.234+27.3i"
The ‘num2str’ function is not very flexible. For better control
over the results, use ‘sprintf’ (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.
See also: sprintf XREFsprintf, int2str XREFint2str,
mat2str XREFmat2str.
-- : int2str (N)
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
⇒
Attr Name Size Bytes Class
==== ==== ==== ===== =====
s 2x7 14 char
This function is not very flexible. For better control over the
results, use ‘sprintf’ (Formatted Output).
Programming Notes:
Non-integers are rounded to integers before display. Only the real
part of complex numbers is displayed.
See also: sprintf XREFsprintf, num2str XREFnum2str,
mat2str XREFmat2str.