str2num - Convert character array or string to numeric array - MATLAB (original) (raw)

Convert character array or string to numeric array

Syntax

Description

[X](#d126e1738947) = str2num([txt](#d126e1738832)) converts a character array or string scalar to a numeric matrix. The input can include spaces, commas, and semicolons to indicate separate elements. Ifstr2num 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' orstr2double to avoid unexpected code execution.

example

[X](#d126e1738947) = str2num([txt](#d126e1738832),Evaluation=[method](#mw%5F2f96827b-8e80-475d-b591-24a547a37293)) determines how txt is evaluated. The default value is"all" and will evaluate any input. SpecifyingEvaluation="restricted" restricts accepted inputs fortxt to basic math expressions such as 200,200+2i, or exp(2).

example

[[X](#d126e1738947),[tf](#d126e1738964)] = str2num([txt](#d126e1738832)) additionally returns a second output argument that is 1 (true) if str2num successfully convertstxt. Otherwise, str2num returns0 (false).

example

Examples

collapse all

Convert character vectors that represent numbers.

X = str2num('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 = datetime 01-Feb-2025 08:07:53

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")

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

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

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

X = str2num('false true true false')

X = 1×4 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')

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

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

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:

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 forchr to basic math expressions.

Output Arguments

collapse all

Output array, returned as a numeric matrix.

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

Extended Capabilities

Version History

Introduced before R2006a

expand all

You can use the name-value argument Evalution="restricted" to restrict accepted inputs to basic math expressions, such as 200,1+2i, or exp(2).