5.4.2 Numerical Data and 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.

 
: str = num2str (x)
: str = num2str (x, precision)
: str = 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.

 
: str = 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.

 
: d = str2double (str)

Convert a string to a real or complex number.

The string must be in one of the following formats where a and b are real numbers and the complex unit is 'i' or 'j':

  • a + bi
  • a + b*i
  • a + i*b
  • bi + a
  • b*i + a
  • i*b + a

If present, a and/or b are of the form [+-]d[,.]d[[eE][+-]d] where the brackets indicate optional arguments and 'd' indicates zero or more digits. The special input values Inf, NaN, and NA are also accepted.

str may be a character string, character matrix, or cell array. For character arrays the conversion is repeated for every row, and a double or complex array is returned. Empty rows in s are deleted and not returned in the numeric array. For cell arrays each character string element is processed and a double or complex array of the same dimensions as str is returned.

For unconvertible scalar or character string input str2double returns a NaN. Similarly, for character array input str2double returns a NaN for any row of s that could not be converted. For a cell array, str2double returns a NaN for any element of s for which conversion fails. Note that numeric elements in a mixed string/numeric cell array are not strings and the conversion will fail for these elements and return NaN.

Programming Note: str2double can replace str2num, is more efficient, and avoids the security risk of using eval on unknown data.

See also: str2num.

 
: x = str2num (s)
: [x, state] = str2num (s)

Convert the string (or character array) s to a number (or an array).

Examples:

str2num ("3.141596")
      ⇒ 3.141596

str2num (["1, 2, 3"; "4, 5, 6"])
      ⇒ 1  2  3
         4  5  6

The optional second output, state, is logically true when the conversion is successful. If the conversion fails the numeric output, x, is empty and state is false.

Caution: As str2num uses the eval function to do the conversion, str2num will execute any code contained in the string s. Use str2double for a safer and faster conversion.

For cell array of strings use str2double.

See also: str2double, eval.

 
: d = bin2dec (str)

Return the decimal number corresponding to the binary number represented by the string str.

For example:

bin2dec ("1110")
     ⇒ 14

Spaces are ignored during conversion and may be used to make the binary number more readable.

bin2dec ("1000 0001")
     ⇒ 129

If str is a string matrix, return a column vector with one converted number per row of str; Invalid rows evaluate to NaN.

If str is a cell array of strings, return a column vector with one converted number per cell element in str.

See also: dec2bin, base2dec, hex2dec.

 
: bstr = dec2bin (d)
: bstr = dec2bin (d, len)

Return a string of ones and zeros representing the conversion of the integer d to a binary number.

If d is a matrix or cell array, return a string matrix with one row for each element in d, padded with leading zeros to the width of the largest value.

The optional second argument, len, specifies the minimum number of digits in the result.

For negative elements of d, return the binary value of the two’s complement. The result is padded with leading ones to 8, 16, 32, or 64 bits as appropriate for the magnitude of the input. Positive input elements are padded with leading zeros to the same width.

Examples:

dec2bin (14)
     ⇒ "1110"

dec2bin (-14)
     ⇒ "11110010"

Programming tip: dec2bin discards any fractional part of the input. If you need the fractional part to be converted too, call dec2base with a nonzero number of decimal places. You can also use fix or round on fractional inputs to ensure predictable rounding behavior.

See also: bin2dec, dec2base, dec2hex.

 
: hstr = dec2hex (d)
: hstr = dec2hex (d, len)

Return a string representing the conversion of the integer d to a hexadecimal (base16) number.

If d is negative, return the hexadecimal complement of d.

If d is a matrix or cell array, return a string matrix with one row for each element in d, padded with leading zeros to the width of the largest value.

The optional second argument, len, specifies the minimum number of digits in the result.

Examples:

dec2hex (2748)
     ⇒ "ABC"

dec2hex (-2)
     ⇒ "FE"

Programming tip: dec2hex discards any fractional part of the input. If you need the fractional part to be converted too, call dec2base with a nonzero number of decimal places. You can also use fix or round on fractional inputs to ensure predictable rounding behavior.

See also: hex2dec, dec2base, dec2bin.

 
: d = hex2dec (str)

Return the integer corresponding to the hexadecimal number represented by the string str.

For example:

hex2dec ("12B")
      ⇒ 299
hex2dec ("12b")
      ⇒ 299

If str is a string matrix, return a column vector with one converted number per row of str; Invalid rows evaluate to NaN.

If str is a cell array of strings, return a column vector with one converted number per cell element in str.

See also: dec2hex, base2dec, bin2dec.

 
: str = dec2base (d, base)
: str = dec2base (d, base, len)
: str = dec2base (d, base, len, decimals)

Return a string of symbols in base base corresponding to the value d.

dec2base (123, 3)
   ⇒ "11120"

If d is negative, then the result will represent d in complement notation. For example, negative binary numbers are in twos-complement, and analogously for other bases.

If d is a matrix or cell array, return a string matrix with one row per element in d, padded with leading zeros to the width of the largest value.

If base is a string then the characters of base are used as the symbols for the digits of d. Whitespace (spaces, tabs, newlines, , etc.) may not be used as a symbol.

dec2base (123, "aei")
   ⇒ "eeeia"

The optional third argument, len, specifies the minimum number of digits in the integer part of the result. If this is omitted, then dec2base uses enough digits to accommodate the input.

The optional fourth argument, decimals, specifies the number of digits to represent the fractional part of the input. If this is omitted, then it is set to zero, and dec2base returns an integer output for backward compatibility.

dec2base (100*pi, 16)
⇒ "13A"
dec2base (100*pi, 16, 4)
⇒ "013A"
dec2base (100*pi, 16, 4, 6)
⇒ "013A.28C59D"
dec2base (-100*pi, 16)
⇒ "EC6"
dec2base (-100*pi, 16, 4)
⇒ "FEC6"
dec2base (-100*pi, 16, 4, 6)
⇒ "FEC5.D73A63"

Programming tip: When passing negative inputs to dec2base, it is best to explicitly specify the length of the output required.

See also: base2dec, dec2bin, dec2hex.

 
: d = base2dec (str, base)

Convert str from a string of digits in base base to a decimal integer (base 10).

base2dec ("11120", 3)
   ⇒ 123

If str is a string matrix, return a column vector with one value per row of str. If a row contains invalid symbols then the corresponding value will be NaN.

If str is a cell array of strings, return a column vector with one value per cell element in str.

If base is a string, the characters of base are used as the symbols for the digits of str. Space (’ ’) may not be used as a symbol.

base2dec ("yyyzx", "xyz")
   ⇒ 123

See also: dec2base, bin2dec, hex2dec.

 
: s = num2hex (n)
: s = num2hex (n, "cell")

Convert a numeric array to an array of hexadecimal strings.

For example:

num2hex ([-1, 1, e, Inf])
⇒ "bff0000000000000
    3ff0000000000000
    4005bf0a8b145769
    7ff0000000000000"

If the argument n is a single precision number or vector, the returned string has a length of 8. For example:

num2hex (single ([-1, 1, e, Inf]))
⇒ "bf800000
    3f800000
    402df854
    7f800000"

With the optional second argument "cell", return a cell array of strings instead of a character array.

See also: hex2num, hex2dec, dec2hex.

 
: n = hex2num (s)
: n = hex2num (s, class)

Typecast a hexadecimal character array or cell array of strings to an array of numbers.

By default, the input array is interpreted as a hexadecimal number representing a double precision value. If fewer than 16 characters are given the strings are right padded with '0' characters.

Given a string matrix, hex2num treats each row as a separate number.

hex2num (["4005bf0a8b145769"; "4024000000000000"])
   ⇒ [2.7183; 10.000]

The optional second argument class may be used to cause the input array to be interpreted as a different value type. Possible values are

OptionCharacters
"int8"2
"uint8"2
"int16"4
"uint16"4
"int32"8
"uint32"8
"int64"16
"uint64"16
"char"2
"single"8
"double"16

For example:

hex2num (["402df854"; "41200000"], "single")
   ⇒ [2.7183; 10.000]

See also: num2hex, hex2dec, dec2hex.

 
: [a, …] = strread (str)
: [a, …] = strread (str, format)
: [a, …] = strread (str, format, format_repeat)
: [a, …] = strread (str, format, prop1, value1, …)
: [a, …] = strread (str, format, format_repeat, prop1, value1, …)

This function is obsolete. Use textscan instead.

Read data from a string.

The string str is split into words that are repeatedly matched to the specifiers in format. The first word is matched to the first specifier, the second to the second specifier and so forth. If there are more words than specifiers, the process is repeated until all words have been processed.

The string format describes how the words in str should be parsed. It may contain any combination of the following specifiers:

%s

The word is parsed as a string.

%f
%n

The word is parsed as a number and converted to double.

%d
%u

The word is parsed as a number and converted to int32.

%*
%*f
%*s

The word is skipped.

For %s and %d, %f, %n, %u and the associated %*s … specifiers an optional width can be specified as %Ns, etc. where N is an integer > 1. For %f, format specifiers like %N.Mf are allowed.

literals

In addition the format may contain literal character strings; these will be skipped during reading.

Parsed word corresponding to the first specifier are returned in the first output argument and likewise for the rest of the specifiers.

By default, format is "%f", meaning that numbers are read from str. This will do if str contains only numeric fields.

For example, the string

str = "\
Bunny Bugs   5.5\n\
Duck Daffy  -7.5e-5\n\
Penguin Tux   6"

can be read using

[a, b, c] = strread (str, "%s %s %f");

Optional numeric argument format_repeat can be used for limiting the number of items read:

-1

(default) read all of the string until the end.

N

Read N times nargout items. 0 (zero) is an acceptable value for format_repeat.

The behavior of strread can be changed via property-value pairs. The following properties are recognized:

"commentstyle"

Parts of str are considered comments and will be skipped. value is the comment style and can be any of the following.

  • "shell" Everything from # characters to the nearest end-of-line is skipped.
  • "c" Everything between /* and */ is skipped.
  • "c++" Everything from // characters to the nearest end-of-line is skipped.
  • "matlab" Everything from % characters to the nearest end-of-line is skipped.
  • user-supplied. Two options: (1) One string, or 1x1 cell string: Skip everything to the right of it; (2) 2x1 cell string array: Everything between the left and right strings is skipped.
"delimiter"

Any character in value will be used to split str into words (default value = any whitespace). Note that whitespace is implicitly added to the set of delimiter characters unless a "%s" format conversion specifier is supplied; see "whitespace" parameter below. The set of delimiter characters cannot be empty; if needed Octave substitutes a space as delimiter.

"emptyvalue"

Value to return for empty numeric values in non-whitespace delimited data. The default is NaN. When the data type does not support NaN (int32 for example), then default is zero.

"multipledelimsasone"

Treat a series of consecutive delimiters, without whitespace in between, as a single delimiter. Consecutive delimiter series need not be vertically "aligned".

"treatasempty"

Treat single occurrences (surrounded by delimiters or whitespace) of the string(s) in value as missing values.

"returnonerror"

If value true (1, default), ignore read errors and return normally. If false (0), return an error.

"whitespace"

Any character in value will be interpreted as whitespace and trimmed; the string defining whitespace must be enclosed in double quotes for proper processing of special characters like "\t". In each data field, multiple consecutive whitespace characters are collapsed into one space and leading and trailing whitespace is removed. The default value for whitespace is " \b\r\n\t" (note the space). Whitespace is always added to the set of delimiter characters unless at least one "%s" format conversion specifier is supplied; in that case only whitespace explicitly specified in "delimiter" is retained as delimiter and removed from the set of whitespace characters. If whitespace characters are to be kept as-is (in e.g., strings), specify an empty value (i.e., "") for "whitespace"; obviously, whitespace cannot be a delimiter then.

When the number of words in str doesn’t match an exact multiple of the number of format conversion specifiers, strread’s behavior depends on the last character of str:

last character = "\n"

Data columns are padded with empty fields or NaN so that all columns have equal length

last character is not "\n"

Data columns are not padded; strread returns columns of unequal length

See also: textscan, sscanf.