How to extract numbers from cell array in MATLAB?
Last Updated :
23 Jul, 2021
In this article, we are going to discuss the extraction of numbers from the cell array with the help of regexp(), str2double(), cat(), and isletter() functions.
What is a Cell Array?
A cell array is nothing but a data type having indexed data containers called cells, where each cell contains any type of data. It mainly contains either a list of texts, combinations of text and numbers, or numeric arrays of different sizes.
Example:
Matlab
% MATLAB code for put data in the cell array
A = {2, 4, 'gfg'}
B = {1, 'GFG', {5; 10; 15}}
Output:

Using regexp( )
The regexp() function is used for matching the regular expression. It is case-sensitive.
Syntax:
startIndex = regexp(str, expression)
[startIndex, endIndex] = regexp(str, expression)
out = regexp(str, expression, outkey)
- startIndex = regexp(str, expression) is used to return the starting index of each substring of str that matches the character patterns specified by the regular expression. If there are no matches, startIndex is an empty array.
- [startIndex,endIndex] = regexp(str,expression) is used to return the starting and ending indices of all matches.
- regexp(str, expression, outkey) is used to return the output specified by the outkey. For example, if outkey is 'match', then this function returns the substrings that match the expression rather than their starting indices.
Example:
Matlab
% MATLAB code for regexp with strjoin()
% Initializing a cell array
A = {'gfg'; 'gfg1.23GFG'; '5gfg10'};
b=regexp(A,'\d+(\.)?(\d+)?','match')
out=strjoin([b{:}],'')
Output:

Using str2double( )
The str2double() function is used for the conversion of strings to double-precision values.
Syntax:
str2double(string)
Here, str2double(string) is used to convert the text in the specified string to double-precision values.
Example:
Matlab
% MATLAB code for regexp() demonstration
% Initializing a cell array
A = {'gfg'; 'gfg1.23GFG'; '5gfg10'};
% Calling the regexp() function over the
% above cell array to extract number part
B = regexp(A,'\d+(\.)?(\d+)?','match');
% Calling the str2double() function to
% convert the text to double-precision values
out = str2double([B{:}])
Output:
out =
1.2300 5.0000 10.0000
Using cat()
The cat() function is used to concatenate the specified arrays.
Syntax:
cat(dim, A, B)
cat(dim, A1, A2 ,…, An)
- cat(dim, A, B) is used to concatenate "B" to the end of "A" along with the dimension "dim" when A and B have compatible sizes, i.e. the lengths of the dimensions match except for the operating dimension dim.
- cat(dim, A1, A2, …, An) is used to concatenate "A1, A2, …, An" along with the dimension dim.
Matlab
% MATLAB code for extract numbers from
% cell using regexp with strcat()
% Initializing a cell array
A = {'gfg'; 'gfg1.23GFG'; '5gfg10'};
A1 = regexp(A,'[\d*\.]*\d*','match')
A2 = [A1{:}]
out = str2double(strcat(A2{:}))
Output:

Using isletter()
The isletter() function is used to find the array of elements that are letters of the alphabet.
Syntax:
isletter('string')
Here, isletter('string') is used to return an array the same size as the specified "string" that contains logical true i.e. 1 when the elements of "string" are letters of the alphabet, and logical false i.e. 0 when they are not.
Example
Matlab
% MATLAB code for isletter() demonstration
% Initializing a cell array
A = {'gfg'; 'gfg1.23GFG'; '5gfg'};
% Calling the cat() function to
% concatenate the data of the above
% cell array into a single string
% one after another
S = cat(2, A{:});
% Calling the isletter() function
% to filter the numeric value
S(isletter(S)) = []
Output:
S = 1.235
Similar Reads
MATLAB Find Exact String in Cell Array Cell arrays in MATLAB store data of various data types as a cell. These cells could contain data of different types but belong to the same array. Now, this article is focused on finding an exact string in a cell array in MATLAB. This can be done easily by using a combination of two MATLAB functions,
2 min read
How to Create a Matrix From a Nested Loop in MATLAB? Matrices are 2-dimensional arrays that store numeric or symbolic data. It is convenient to create them with the help of nested loops as the outer loop creates the elements along one dimension and the inner loop creates the elements along the second dimension. In this article, we will see how to crea
2 min read
How to find sum of elements of an array in MATLAB? This article will discuss the "Finding sum of elements of an array" in MATLAB that can be done using multiple approaches which are illustrated below. Â Using sum(A)Â This is used to return the sum of the elements of the array along the first array dimension whose size does not equal 1. It returns a ro
4 min read
How To Export a Matrix as a CSV File in MATLAB? A CSV file - Comma Separated File, as the name suggests, is a file that stores data delimited by a ' , '. In MATLAB, there is a simple way of exporting a matrix to a csv file. The writematrix() function is used to write a matrix into any desired file type. The syntax is writematrix(<matrix_name
2 min read
How to reverse a number in MATLAB? In this article, we will discuss the "Reversing of a number" in MATLAB that can be done using the multiple methods which are illustrated below. Using str2num() The str2num() function is used to convert the specified character array or string to a numeric array. Syntax: Â str2num(chr) Parameters: This
3 min read
How to Find Index of Element in Array in MATLAB? In MATLAB, the arrays are used to represent the information and data. You can use indexing to access the elements of the array. Â In MATLAB the array indexing starts from 1. To find the index of the element in the array, you can use the find() function. Using the find() function you can find the indi
4 min read
How to create a function in MATLAB ? A function is a block of statements that intend to perform a specific task. Functions allow the users to reuse the code frequently. MATLAB has several predefined functions which are ready to use such as sin(), fact(), cos() etc. MATLAB also allows the users to define their own functions. Syntax: fun
2 min read
Create Array of Zeros in MATLAB MATLAB generally stores its variables in matrix forms, also in array and vector form. Sometimes, we often need a matrix(or array or vector) of zero(s) for some specific operations. We can create a matrix of zero(s) manually or with the help of the in-built function of MATLAB. The in-built function t
5 min read
MATLAB Find Closest Value in Array MATLAB arrays store numbers and allow users to perform various operations on them. In this article, we shall see how to find the closest value to a given target value in a MATLAB array. The same task can be performed by using the nearest neighbor interpolation, which we shall explore in the followin
2 min read
How to Create a New Matrix From All Possible Row Combinations in MATLAB? A  matrix represents a collection of numbers arranged in an order of rows and columns. A matrix encloses the elements in parentheses or brackets.  We will learn how to create a new matrix from all possible row combinations of rows of a different matrix and display the combinations. Procedure of Mak
3 min read