Documentation

str2double

Convert string to double precision value

Syntax

X = str2double(str)

Description

example

X = str2double(str)converts the text instrto double precision values.strcontains text that represents real or complex numeric values.strcan be a character vector, a cell array of character vectors, or a string array. Ifstris a character vector or string scalar, thenXis a numeric scalar. Ifstris a cell array of character vectors or a string array, thenXis a numeric array that is the same size asstr.

r的文本epresents a number can contain digits, a comma (thousands separator), a decimal point, a leading+or-sign, anepreceding a power of 10 scale factor, and anior ajfor a complex unit. You cannot use a period as a thousands separator, or a comma as a decimal point.

Ifstr2doublecannot convert text to a number, then it returns aNaNvalue.

Examples

collapse all

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.

X = str2double('3.1416')
X = 3.1416

Convert a character vector when it represents a number using exponential notation.

X = str2double('2.998e8')
X = 299800000

Convert a character vector that uses a comma as the thousands separator.

X = str2double('1,200.34')
X = 1.2003e+03

Convert a character vector to a complex number.

X = str2double('3.14 + 2.7i')
X = 3.1400 + 2.7000i

Convert a cell array of character vectors to a numeric array.

str = {'2.718','3.1416';'137','0.015'}; X = str2double(str)
X =2×22.7180 3.1416 137.0000 0.0150

Starting in R2016b, you can create string arrays using thestringfunction. You can convert strings to numbers using thestr2doublefunction.

Create a string that represents a number. Convert it to a numeric scalar.

str = string('81470.5')
str = "81470.5"
X = str2double(str)
X = 8.1471e+04

Create a string array representing numbers. Convert it to a numeric array that is the same size.

str = string({“91.57”,'95.95','3.57';'79.22','65.57','84.91'})
str =2x3 string array"91.57" "95.95" "3.57" "79.22" "65.57" "84.91"
X = str2double(str)
X =2×391.5700 95.9500 3.5700 79.2200 65.5700 84.9100

Input Arguments

collapse all

r的文本epresents numbers, specified as a character vector, a cell array of character vectors, or a string array.

Extended Capabilities

Introduced before R2006a

Was this topic helpful?