5.3.2 Converting Numerical Data to 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).

: 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, num2str, int2str.

: 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 (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.

See also: sprintf, int2str, mat2str.

: 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
  ⇒ 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.

See also: sprintf, num2str, mat2str.

© 1996–2020 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/v6.3.0/Converting-Numerical-Data-to-Strings.html