Convert strings into numeric values.
str2double
can replacestr2num
, but avoids the use ofeval
on unknown data.str can be the form ‘[+-]d[.]dd[[eE][+-]ddd]’ in which ‘d’ can be any of digit from 0 to 9, and ‘[]’ indicate optional elements.
num is the corresponding numeric value. If the conversion fails, status is -1 and num is NaN.
status is 0 if the conversion was successful and -1 otherwise.
strarray is a cell array of strings.
Elements which are not defined or not valid return NaN and the status becomes -1.
If str is a character array or a cell array of strings, then num and status return matrices of appropriate size.
str can also contain multiple elements separated by row and column delimiters (cdelim and rdelim).
The parameters cdelim, rdelim, and ddelim are optional column, row, and decimal delimiters.
The default row-delimiters are newline, carriage return and semicolon (ASCII 10, 13 and 59). The default column-delimiters are tab, space and comma (ASCII 9, 32, and 44). The default decimal delimiter is ‘.’ (ASCII 46).
cdelim, rdelim, and ddelim must contain only nul, newline, carriage return, semicolon, colon, slash, tab, space, comma, or ‘()[]{}’ (ASCII 0, 9, 10, 11, 12, 13, 14, 32, 33, 34, 40, 41, 44, 47, 58, 59, 91, 93, 123, 124, 125).
Examples:
str2double ("-.1e-5") -1.0000e-006 str2double (".314e1, 44.44e-1, .7; -1e+1") 3.1400 4.4440 0.7000 -10.0000 NaN NaN line = "200, 300, NaN, -inf, yes, no, 999, maybe, NaN"; [x, status] = str2double (line) x = 200 300 NaN -Inf NaN NaN 999 NaN NaN status = 0 0 0 0 -1 -1 0 -1 0