Navigation

Operators and Keywords

Function List:

C++ API

: str2double (s)

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.

s 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 s 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.

str2double can replace str2num, and it avoids the security risk of using eval on unknown data.

See also: str2num.

Package: octave