Main Content

str2num

Convert character array or string to numeric array

Description

X = str2num(txt) converts a character array or string scalar to a numeric matrix. The input can include spaces, commas, and semicolons to indicate separate elements. If 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 eval which might lead to undesired side effects. When calling str2num with untrusted user input, use Evaluation='restricted' or str2double to avoid unexpected code execution.

example

X = str2num(txt,Evaluation=method) determines how txt is evaluated. The default value is "all" and will evaluate any input. Specifying Evaluation="restricted" restricts accepted inputs for txt to basic math expressions such as 200, 200+2i, or exp(2).

example

[X,tf] = str2num(txt) additionally returns a second output argument that is 1 (true) if str2num successfully converts txt. Otherwise, str2num returns 0 (false).

example

Examples

collapse all

Convert character vectors that represent numbers.

X = str2num('100')
X = 
100
X = str2num('100 200 300 400')
X = 1×4

   100   200   300   400

str2num interprets exponential notation.

X = str2num('12e-3 5.9e-3 -8.1e-3 2.56e-3; 5 11.2 17.9 33')
X = 2×4

    0.0120    0.0059   -0.0081    0.0026
    5.0000   11.2000   17.9000   33.0000

Use the name-value argument Evaluation="restricted" to restrict accepted inputs to basic math expressions.

When Evaluation is not set, str2num will evaluate any input.

X = str2num("datetime")
X = datetime
   05-Sep-2024 14:27:51

Specify Evaluation="restricted" to restrict accepted inputs to basic math expressions. Inputs that are not basic math expressions will return [] instead.

X = str2num("datetime",Evaluation="restricted")
X =

     []

Convert a character vector to an unsigned 16-bit integer using str2num and uint16.

X = str2num('256');
X = uint16(X)
X = uint16

256

Convert a character vector containing true and false to a logical array.

X = str2num('false true true false')
X = 1x4 logical array

   0   1   1   0

Return the status of a conversion that fails. tf is 0, and X is an empty matrix.

[X,tf] = str2num('12e-3 m/s, 5.9e-3 m/s')
X =

     []
tf = logical
   0

If you remove the extra text (m/s), then conversion succeeds.

[X,tf] = str2num('12e-3 5.9e-3')
X = 1×2

    0.0120    0.0059

tf = logical
   1

Input Arguments

collapse all

Representation of a numeric matrix, specified as a character array or string scalar.

Text that represents a numeric matrix can contain spaces, commas, or semicolons, such as '5', '10,11,12', or '5,10;15,20'. In addition to numeric values and delimiters, input text also can include any of the following items:

  • A decimal point

  • Leading + or - signs

  • The letter e or d preceding a power of 10 scale factor

  • The letter i or j indicating a complex or imaginary number

  • true or false indicating logical values

Space characters, or the lack of them, can be significant. For instance, str2num('1+2i') and str2num('1 + 2i') both return the complex number 1.0000 + 2.0000i, while str2num('1 +2i') returns the 1-by-2 vector [1.0000 + 0.0000i 0.0000 + 2.0000i]. To avoid this problem, use the str2double function.

str2num converts character arrays and string scalars only. To convert nonscalar string arrays or cell arrays to numeric arrays, use the str2double function.

Evaluation method, specified as "all" or "restricted". Setting evaluation method to "restricted" restricts accepted inputs for chr to basic math expressions.

Output Arguments

collapse all

Output array, returned as a numeric matrix.

True or false result, returned as a 1 or 0 of data type logical.

Extended Capabilities

Version History

Introduced before R2006a

expand all