str2double
Convert strings to double precision values
Syntax
Description
example
X = str2double[str] converts the text in str to double precision values. str contains text that represents real or complex numeric values. str can be a character vector, a cell array of character vectors, or a string array. If str is a character vector or string scalar, then X is a numeric scalar. If str is a cell array of character vectors or a string array, then X is a numeric array that is the same size as str.
Text that represents a number can contain digits, a comma [thousands separator], a decimal point, a leading + or - sign, an e preceding a power of 10 scale factor, and an i or a j for a complex unit. You cannot use a period as a thousands separator, or a comma as a decimal point.
If str2double cannot convert text to a number, then it returns a NaN value.
Examples
collapse all
Convert Character Vectors to Numbers
Convert character vectors that represent numbers to double precision values. You can also convert a cell array of character vectors to a numeric array of the same size.
Convert a character vector to a real number.
Convert a character vector when it represents a number using exponential notation.
Convert a character vector that uses a comma as the thousands separator.
Convert a character vector to a complex number.
Convert a cell array of character vectors to a numeric array.
Convert String Arrays to Numeric Arrays
Starting in R2016b, you can create string arrays using the string function. You can convert strings to numbers using the str2double function.
Create a string that represents a number. Convert it to a numeric scalar.
Create a string array representing numbers. Convert it to a numeric array that is the same size.
Input Arguments
collapse all
str Text that represents numbers
character vector | cell array of character vectors | string array
Text that represents numbers, specified as a character vector, a cell array of character vectors, or a string array.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
This function fully supports tall arrays. For more information, see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder.
Usage notes and limitations:
Input text must be a string scalar or a character vector.
Generated code always returns a complex result.
Generated code results can differ from MATLAB® results for misplaced commas [commas that are not used as thousands separators].
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox ThreadPool.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox.
Usage notes and limitations:
strmust be a string array or a cell array of character vectors.
For more information, see Run MATLAB Functions with Distributed Arrays [Parallel Computing Toolbox].
See Also
char | hex2num | num2str | str2num | isnumeric | isa | double