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
Software Development Life Cycle (SDLC) Software development life cycle (SDLC) is a structured process that is used to design, develop, and test good-quality software. SDLC, or software development life cycle, is a methodology that defines the entire procedure of software development step-by-step. The goal of the SDLC life cycle model is
11 min read
Waterfall Model - Software Engineering The Waterfall Model is a Traditional Software Development Methodology. It was first introduced by Winston W. Royce in 1970. It is a linear and sequential approach to software development that consists of several phases. This classical waterfall model is simple and idealistic. It is important because
13 min read
What is Software Testing? Software testing is an important process in the Software Development Lifecycle(SDLC). It involves verifying and validating that a Software Application is free of bugs, meets the technical requirements set by its Design and Development, and satisfies user requirements efficiently and effectively.Here
11 min read
What is DFD(Data Flow Diagram)? Data Flow Diagram is a visual representation of the flow of data within the system. It help to understand the flow of data throughout the system, from input to output, and how it gets transformed along the way. The models enable software engineers, customers, and users to work together effectively d
9 min read
COCOMO Model - Software Engineering The Constructive Cost Model (COCOMO) It was proposed by Barry Boehm in 1981 and is based on the study of 63 projects, which makes it one of the best-documented models. It is a Software Cost Estimation Model that helps predict the effort, cost, and schedule required for a software development project
15+ min read
What is Spiral Model in Software Engineering? The Spiral Model is one of the most important SDLC model. The Spiral Model is a combination of the waterfall model and the iterative model. It provides support for Risk Handling. The Spiral Model was first proposed by Barry Boehm. This article focuses on discussing the Spiral Model in detail.Table o
9 min read
Software Requirement Specification (SRS) Format In order to form a good SRS, here you will see some points that can be used and should be considered to form a structure of good Software Requirements Specification (SRS). These are below mentioned in the table of contents and are well explained below. Table of ContentIntroductionGeneral description
5 min read
Differences between Black Box Testing and White Box Testing In the Software Testing field, various methods are used to find defects, which used to increasing the software's quality. Black-Box Testing and White-Box Testing play important roles in these process.Let's Learn about them in detail.Table of ContentWhat is Black Box Testing?What is White Box Testing
6 min read
Software Testing Life Cycle (STLC) The Software Testing Life Cycle (STLC) is a process that verifies whether the Software Quality meets the expectations or not. STLC is an important process that provides a simple approach to testing through the step-by-step process, which we are discussing here. Software Testing Life Cycle (STLC) is
7 min read
Coupling and Cohesion - Software Engineering The purpose of the Design phase in the Software Development Life Cycle is to produce a solution to a problem given in the SRS(Software Requirement Specification) document. The output of the design phase is a Software Design Document (SDD). Coupling and Cohesion are two key concepts in software engin
10 min read