str2double - Convert strings to double precision values - MATLAB (original) (raw)
Main Content
Convert strings to double precision values
Syntax
Description
Note
Calling string and then double is recommended overstr2double
because it provides greater flexibility and allows vectorization. For additional information, see Alternative Functionality.
X = str2double([str](#bvf7xtc-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. Ifstr
is a character vector or string scalar, thenX
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, ane
preceding a power of 10 scale factor, and ani
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
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.
X = str2double('2.998e8')
Convert a character vector that uses a comma as the thousands separator.
X = str2double('1,200.34')
Convert a character vector to a complex number.
X = str2double('3.14 + 2.7i')
Convert a cell array of character vectors to a numeric array.
str = {'2.718','3.1416'; '137','0.015'}; X = str2double(str)
X = 2×2
2.7180 3.1416
137.0000 0.0150
You can convert text 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.
str = ["292.1" "139.44" ".241" "0" "100.245"]
str = 1×5 string "292.1" "139.44" ".241" "0" "100.245"
X = 1×5
292.1000 139.4400 0.2410 0 100.2450
Input Arguments
Text that represents numbers, specified as a character vector, a cell array of character vectors, or a string array.
Alternative Functionality
Update code that makes use of str2double
to use string and then double instead. Using double
to convert achar
array produces an array of the corresponding Unicode® values. Text in strings does not convert in this way. For example:
Not Recommended | Recommended |
---|---|
x = str2double('13') x = 13 | x = double(string('13')) x = 13 |
Extended Capabilities
Thestr2double
function fully supports tall arrays. For more information, see Tall Arrays.
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).
Version History
Introduced before R2006a