| MATLAB Function Reference | ![]() |
x = str2num('str')
[x status] = str2num('str')
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
Digits
A decimal point
A leading + or - sign
A letter e or d preceding a power of 10 scale factor
A letter i or j indicating a complex or imaginary number.
[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. If the input string str does not represent a valid number or matrix, the MATLAB® software sets x to the empty matrix. If the conversions fails, status is set to 0.
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.
Note str2num uses the eval function to convert the input argument, so side effects can occur if the string contains calls to functions. Use str2double to avoid such side effects, or when the input to str2num contains a string that represents a single number. |
str2num('3.14159e0') is approximately π.
To convert a string matrix,
str2num(['1 2';'3 4'])
ans =
1 2
3 4num2str, hex2num, sscanf, sparse, special characters
![]() | str2mat | strcat | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |