Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

str2num

Convert character array to numeric array

Syntax

X = str2num(chr)
[X,tf] = str2num(chr)

Description

example

X = str2num(chr) converts a character array 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 string or cell arrays, and is sensitive to spacing around + and - operators. In addition, str2num uses the eval function, which can cause unintended side effects when the input includes a function name. To avoid these issues, use str2double.

example

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

Examples

collapse all

Convert character vectors that represent numbers.

X = str2num('100')
X = 100
X = str2num('100 200 300 400')
X = 

   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 = 

    0.0120    0.0059   -0.0081    0.0026
    5.0000   11.2000   17.9000   33.0000

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 = 

    0.0120    0.0059

tf = logical
   1

Input Arguments

collapse all

Representation of a numeric matrix, specified as a character array.

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 only. To convert string or cell arrays to numeric arrays, use the str2double function.

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.

Introduced before R2006a

Was this topic helpful?