str2num

Convert string to number

Syntax

x = str2num('str')
[x, status] = str2num('str')

Description

    Note   str2num uses the eval function to convert the input argument. Side effects can occur if the string contains calls to functions. Using str2double can avoid some of these side effects.

x = str2num('str') converts the string str, which is an ASCII character representation of a numeric value, to numeric representation. str2num also converts string matrices to numeric matrices. If the input string does not represent a valid number or matrix, str2num(str) returns the empty matrix in x.

The input string can contain one or more numbers separated by spaces, commas, or semicolons, such as '5', '10,11,12', or '5,10;15,20'. In addition to numerical values and delimiters, the input string can also include a decimal point, leading + or - signs, the letter e or d preceding a power of 10 scale factor, or the letter i or j indicating a complex or imaginary number.

The following table shows several examples of valid inputs to str2num:

String InputNumeric OutputOutput Class
'500'500double
'500 250 125 67'500 250 125 671-by-4 row vector of double
'500; 250; 125; 62.5'500.0000
250.0000
125.0000
  62.5000
4-by-1 column vector of double
'1 23 6 21; 53:56'  1 23   6 21
53 54 55 56
2-by-4 matrix of double
'12e-3 5.9e-3'0.0120 0.00591-by-2 row vector of double
'uint16(500)'50016–bit unsigned integer

    Note:   str2num does not operate on cell arrays. To convert a cell array of strings to a numeric value, use str2double.

If the input string does not represent a valid number or matrix, str2num(str) returns the empty matrix in x.

[x, status] = str2num('str') returns the status of the conversion in logical status, where status equals logical 1 (true) if the conversion succeeds, and logical 0 (false) otherwise.

Space characters can be significant. For instance, str2num('1+2i') and str2num('1 + 2i') produce x = 1+2i, while str2num('1 +2i') produces x = [1 2i]. You can avoid these problems by using the str2double function.

Examples

Input a character string that contains a single number. The output is a scalar double:

A = str2num('500')
A =
   500

class(A)
ans =
   double

Repeat this operation, but this time using an unsigned 16–bit integer:

A = str2num('uint16(500)')
A =
    500

class(A)
ans =
   uint16

Try three different ways of specifying a row vector. Each returns the same answer:

str2num('2 4 6 8')              % Separate with spaces.
ans =
     2     4     6     8

str2num('2,4,6,8')              % Separate with commas.
ans =
     2     4     6     8

str2num('[2 4 6 8]')            % Enclose in brackets.
ans =
     2     4     6     8

Note that the first two of these commands do not need the MATLAB® square bracket operator to create a matrix. The str2num function inserts the brackets for you if they are needed.

Use a column vector this time:

str2num('2; 4; 6; 8')
ans =
     2
     4
     6
     8

And now a 2-by-2 matrix:

str2num('2 4; 6 8')
ans =
     2     4
     6     8
Was this topic helpful?