MATLAB has various functions to convert data from one type to another, like all programming languages.

Next, we will explain everything about this function, its syntax, call modes, input and output arguments, and the accepted data types. We have also included images and practical examples in this article that show how to use this feature.

## Syntax of the MATLAB Function str2double()

d = str2double (str)

## MATLAB str2double() Function Description

The str2double() function is part of a set of data type conversion functions in MATLAB. The str2double() returns in “d” the numeric representation in double precision of the string sent in “str”. Double precision conversion of text to numeric can be done with this function for real and complex values. The strings sent in “str” can be the type cell array of character vectors, character vectors, or an array of strings.

The MATLAB function str2double() accepts a reduced set of characters other than numeric characters to represent signs, complex numbers, commas, decimal points, and so on. Next, let us look at a list of these special characters that this function accepts in its input string.

**Comma “,”**: this character can be used as a thousand separators. For example, in this string representing the number “32,000”, the comma was used to separate the tens and ones digits of the thousands from the hundreds, tens, and units. The use of this character is optional since its use or non-use does not change the result of the conversion.

**Dot “.”**: This character is used in values with decimal places to separate the integer part from the decimal part.

**Addition and subtraction “+” and “-“**: These special characters are used to set the sign of the values displayed in the input string of str2double.

**“i” and “j”**: These additional characters represent real and imaginary quantities in complex values.

**“e”**: This character in the input string of str2double() represents exponential values before the power factor.

If an unaccepted character is sent in the input string, an error prevents str2double() from performing the conversion. In such cases, this function returns a NaN value. Next, we will see some practical examples that show the use of this function in MATLAB.

## How To Convert a String to Numeric Double Scalar With MATLAB str2double Function

In this example, we see how you can convert a string from a positive integer to its numeric representation. The result is a double precision scalar representing the value sent in “str” in string format.

d = str2double (str)

d = class (d)

d=

12345

ans =

'double'

The following illustration shows how the str2double() function in “d” returns a double-precision integer scalar containing the numeric value contained in the string “str” sent as an input argument. The class() function displays the data type of “d”.

## How To Convert a String to a Double Scalar With a Decimal Point Using the MATLAB Function str2double()

The MATLAB function str2double() accepts a limited number of characters representing values in the input string, one of which is the decimal point. In this example, we will look at how you can convert strings representing numbers with a decimal point to a double-precision scalar.

d = str2double (str)

d = class (d)

d =

3.1415

d =

'double'

The following image shows that the dot in the input string is also converted by the str2double() function:

## Converting a String With a Negative Sign Value Into a Double Scalar With the MATLAB Function str2double

As explained previously, this function accepts a limited number of characters representing values in the input string. Other non-numeric characters that the MATLAB function str2double() accepts are the “+” and “-” signs. Next, let us look at an example in which we implement these symbols to represent positive and negative values in the string that serves as the input argument of str2double().

d = str2double (str)

d =

'double'

-54321

As shown in the following figure, the “+” and “-” characters in the input string determine the sign of the result after conversion:

## Conversion of Strings With Complex Numbers Into a Double Scalar With the MATLAB Function str2double

This example shows how to convert a string of text representing a complex number into a double-precision complex scalar using the MATLAB function str2double. In the text of the input string, we need to insert the character “i” to denote the real and imaginary parts of the number.

d = str2double (str)

d =

'double'

24.2325 - 1.2748i

As the following image in the command console shows, str2double has performed the conversion of the input string to a complex number. When displaying these values, no spaces must be inserted between the real and imaginary parts of the complex number.

## Converting an Array of Character Vectors to a Numeric Array Using the str2double Function in MATLAB

This example shows how to convert an array of character vectors into a numeric array using the MATLAB function str2double. To do this, an array of character vectors “a” with 2×2 elements is created and sent as an input argument.

'1364', '1414'};

d = str2double (a)

d =

1321 7748

1364 1414

## Conclusion

In this article, we explained how to use the MATLAB function str2double. This function is a set of features that this powerful scientific programming language provides to the programmer for converting from one type of data to another. The str2double function is beneficial for obtaining numeric data from text files, databases, or other input data where the information is formatted as text or strings. For your better understanding, we have added practical examples and images to this article that better explain how to use this function with different input arrays. We have also included in this article a list of the non-numeric additional characters accepted by this function. We hope you found this MATLAB article helpful. See other Linux Hint articles for more tips and information.