Convert character array or string to numeric array
converts a character array or string scalar to a numeric matrix. The input can
include spaces, commas, and semicolons to indicate separate elements. If
X
= str2num(chr
)str2num
cannot parse the input as numeric values, then it
returns an empty matrix.
The str2num
function does not convert cell arrays or
nonscalar string arrays, and is sensitive to spacing around +
and
-
operators.
Note
Security Considerations:
str2num
is implemented using the
eval
function, which evaluates the input argument.
When calling str2num
with untrusted user input,
validate the input to avoid unexpected code execution. Examples of untrusted
user input are data coming from a user you might not know or from a source
you have no control over. If you need to address this concern, consider
these approaches:
Validate inputs to str2num
. First, search for
allowed operations, such as the use of ()
,
/
, *
,
+
, -
, and
.
, and the letters e
and
i
. Then, if you find other operations,
disallow execution.
Replace str2num
with a function such as
str2double
.
Unintended Side Effects: In addition,
there can be other unintended side effects when the input includes a
function name. To avoid these issues, use
str2double
.