str2double

Converts text to double precision numbers, returning NaN where the text is not a number.

Category: Language Fundamentals > Data Types

Syntax

X = str2double(str)

Converts a character vector, string, or each element of a string array or cell array.

Description

Unlike str2num, str2double never evaluates code, so it is safe for user-supplied input. It accepts an optional sign, decimal point, exponent (1e3), Inf and NaN, surrounding spaces, and thousands separators such as 1,000.

Non-text input such as a numeric value returns NaN. Failure is silent: check with isnan when you need to detect bad input.

Input arguments

str
Text to convert: character vector, string array, or cell array of character vectors.

Output arguments

X
Double array with one value per input text; NaN for text that cannot be read as a number.

Example

Valid and invalid text

str2double('3.14')
str2double({'1e3','abc','-2'})
str2double("1,000")
Output
ans = 3.1400
ans = 1×3

        1000         NaN          -2
ans = 1000

See also

str2num num2str string sscanf isnan