
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Extract Numbers from Cell Array in MATLAB
MATLAB is an efficient tool to perform operations on arrays and matrices. In MATLAB, there are several built-in functions available for cell indexing and conversion that can be used to extract numbers from a cell array. Before proceeding further, let us first get an overview of cell array in MATLAB.
What is a Cell Array in MATLAB?
In MATLAB, a type of data structure that is used to store data of different data types and sizes is called a cell array. In other words, a cell array is a type of data structure that can hold mix of different data types. For example, a cell array can hold numbers, strings, etc. together.
Hence, in MATLAB, the cell array is one of the powerful tools that enables us to handle heterogeneous data.
Syntax
In MATLAB, to create a cell array, we can use curly braces "{}" or the "cell" function.
For example, creating a cell array using "{}":
cell_array = {'Tutorial', 2006, [7 8 9]};
Similarly, creating a cell array using the "cell" function:
cell_array = cell(3, 3);
Now, let us understand how we can extract numbers from a cell array containing mixed data types in MATLAB.
Extract Numbers from Cell Array in MATLAB
In MATLAB, we can follow the following steps to extract numbers from a cell array containing mixed data types:
Step (1) Create or load the cell array containing mixed data types.
Step (2) Create an empty array to hold the extracted numbers from the cell array.
Step (3) Use a suitable loop to go through the elements of the cell array. Program this loop so that it can check whether the element is a number or not. If it is a number, then add it to the empty array created in the previous step.
Step (4) Display the extracted numbers as output.
Hence, extracting numbers from a cell array in MATLAB is a straightforward process.
Example
Let us now take an example to understand how to implement code in MATLAB to extract numbers from a cell array.
% MATLAB code to extract numbers from a cell array % Create a sample cell array containing mixed data types cell_array = {22, 'TutorialsPoint', 9.81, 'eBooks', [10 12]}; % Create an empty array to store extracted numbers numbers = []; % Define a loop to extract numbers from the cell array for i = 1:numel(cell_array) if isnumeric(cell_array{i}) numbers = [numbers, cell_array{i}]; end end % Display the result disp('Extracted numbers from the cell array are:'); disp(numbers);
Output
Extracted numbers from the cell array are: 22.0000 9.8100 10.0000 12.0000
Code Explanation
In this MATLAB program, we start by creating a sample cell array "cell_array" containing mixed data types. Then, we create an empty array "numbers" to store the numbers extracted from the cell array.
After that we define a "for" loop to check identify and extract the numbers from the cell array. In this loop, we use the "isnumeric" function to check if the element of the cell array is a number or not. If the element is a number, then we extract it and store in the numeric array.
Finally, we use the "disp" function to display the extracted numbers.
Conclusion
In conclusion, a cell array is a data structure that can store different kinds of data together. In MATLAB, we can extract numeric data types from a cell array by using a loop mechanism.
In this tutorial, I have explained the step-by-step process and an example in MATLAB to extract numbers from a cell array. You can try this MATLAB code with different cell arrays containing different data values.