## Introduction to Matlab String to Number

In Matlab, str2num and str2double function is used for a string to number conversion. The function is used to convert text or string to a numeric value called as str2num function. The str2num function supports one or more numbers separated by spaces, decimal point, and commas. The input string can also include signs like the ‘+’ sign and ‘–‘ sign, the letter e or d preceding power of 10 scales factor and letter i or j for indicating the complex or imaginary number.str2num also converts string matrix to numeric matrix. If the input string does not contain a valid number in the matrix, str2num(str) returns the empty matrix.

**Syntax**

The syntax for Matlab string to numbers as shown below:-

- x1 = str2num(chr)
- [ x1,tf] = str2num(chr)
- x1 = str2double(str)

### How to Domatlab String to Number?

Basically, now we can see how to convert a string into numbers for this, we can use a Matlab command str2num, the cell arrays, non-scalar string arrays it does not convert into numerical data type using a str2num function, and it is also sensitive spacing around to operators to avoid this error we use a str2double command. The steps to convert a string into a number are as follows:-

**Step 1:** First, we take a char string into a variable

**Step 2:** use a command to convert a string into a number

**Step 3:** it’s an optional step to check the data type of object; this step we can perform before converting a string ** **

### Examples of Matlab String to Number

Different examples are mentioned below:

#### Example #1

In this example, we have seen that how to convert string data to a number data type, so firstly we write a variable ‘a1’, and in that variable, we write anything or numbers in our case, we write numbers, the numbers are 15 81022 in single inverted, the numbers are assigned to variable ‘a1’. Then we find its class for that we write a class function with a variable name, class (a1). It’s a character, but actually, it’s a string of characters. Then we convert this string into the integer or numerical data type; the command converted to integer is str2num, str2num simply means that we are converting string to the numerical data type, so we simply write str2num(a1). And then we see a result in the numbers, so for verification, we see its data type with the command class, as we saw class returns the class of that given object and then we can verify that it converts the string data into a numerical data type or an integer data type. So we write a class of ans because now the answer is in ans, so we write a class (and), and we can see a data type; it will be numerical data type.

**Code:**

`clc;`

clear all;

close all;

a1 = '15781022'

class (a1)

str2num (a1)

class (ans)

**Output:**

As we saw a result on the command window, the first data type is char that is nothing but a string of character, and then after applying str2num, you see a data type as double it is nothing but numerical data type. So we prove that the string is converted into numbers.

#### Example #2

Let us see an example; in this example, we pass a string with some alphabets and special character; then in 1^{st} case, we have seen the result as x1 is an empty matrix and tf1 is a 0 it indicates that the status of conversion that fails. The cell arrays, non-scalar string arrays it does not convert into numerical data type using a str2num function, and it is also sensitive spacing around to operators. So in the 2^{nd} statement, we remove that alphabets and special character into that string, then str2num function returns ax1 is not an empty matrix, written numbers on it, and the tf1 is ‘1’ it indicates that the conversion is successful.

**Code:**

`clc;`

clear all;

close all;

[ x1,tf1 ] = str2num('123 m/s, 593 m/s')[ x1,tf1 ] = str2num('123, 593')

**Output:**

As we saw a result, there is a drawback of str2num function, the cell arrays, non-scalar string arrays it does not convert into numerical data type using a str2num function, and it is also sensitive spacing around to operators or if we used then the conversion is failed, or it will return empty matrix. So this can avoid if we use a str2double function for conversion.

#### Example #3

In this example, we convert the string call into double-precision values using a str2double command. First, we take a string cell into an str1 variable, then we check a data type of str1 using a class function, and then we convert that cell into a numerical data type by using a str2double command, and for verification purposes, we again use a class function.

**Code:**

`clc;`

clear all;

close all;

str1 = string({'10', '55', '88', '8', '87'; '88', '456', '100', '65', '5'})class (str1)x1 = str2double(str1)class (x1)

**Output:**

### Conclusion

In this article, we saw the concept of string to number conversion, basically to convert string to number str2num and str2double function is used. Then saw syntax related to string to number conversion statements and how it’s used in Matlab code. Also, we saw some examples related to the conversion of a string to a number.

### Recommended Articles

This is a guide to Matlab String to Number. Here we discuss How to Domatlab String to Number and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –