Find the Exact String in a Cell Array in MATLAB


MATLAB is a programming environment developed for scientists and engineers to design and analyze system, perform data analysis, create visualizations, and more. MATLAB stands for Matrix Laboratory, it is a programming and interactive platform developed by MathWorks to provide a variety of tools for programming mathematical functions and operations, data analysis, etc. MATLAB is extensively used in different field of science, engineering, finance, economics, and more.

Read this article to understand the process of finding the exact string in a cell array in MATLAB.

What is a Cell Array in MATLAB?

In MATLAB, a cell array is a data structure in which each element of the array can contain a data of different data types. Therefore, in a cell array, each element can contain a mix of different data types like integers, floating point numbers, characters, other arrays, etc.

In MATLAB, we can define a cell in the following two syntaxes −

A = {'banana', 5.215, [1, 4]}

Or

A = cell({'banana', 5.215, [1, 4]})

Here, the cell array "A" contains three elements of different data types as,

  • The first element is a string "banana".

  • The second element is a floating-point number "5.215".

  • The third element is a normal array with values [1, 4].

Now, let us discuss about finding the exact string in a cell array.

Find Exact String in Cell Array in MATLAB

In MATLAB, we can use the following two combinations of two MATLAB functions to find an exact string in a cell array.

  • strcmp() and find() functions.

  • ismember() and find() functions.

Let us see how we use these function combinations in MATLAB to find an exact string in a cell array with the help of program examples.

Method 1: Using strcmp() and find() Functions

In MATLAB, we can find an exact string in a cell array using the combination of strcmp() and find() functions.

Example 1

The following MATLAB program demonstrates the process of finding an exact string in a cell array using the combination of strcmp() and find() functions.

% Create a cell array of strings
A = {'Tutorials', 'Point', 'for', 'MATLAB'}
% Choose a target string
target_string= 'MATLAB'
% Get a logical array from strcmp
strcmp(A, target_string)
% Find the index of the target string
index = find(strcmp(A, target_string))
% Check if the target string is found
if ~isempty(index)
   fprintf('The target string is found at index %d.
', index) else fprintf('The target string is not found.
') end

Output

It will produce the following output

A = 1×4 cell array
{'Tutorials'}    {'Point'}    {'for'}    {'MATLAB'}
target_string = 'MATLAB'
ans = 1×4 logical array
0	0	0	1
index = 4
The target string is found at index 4.

Explanation

In this MATLAB code, we have first created a cell array "A" with four strings "{'Tutorials', 'Point', 'for', 'MATLAB'}". Then, we have selected a target string of "MATLAB". After that, we have used the "strcmp()" function to return a logical array where each element is true (logical 1) if the corresponding element of the cell array is equal to the target string. Then, we have used the "find()" function to find the index of the true (logical 1) element in the logical array that corresponds to the index of the target string in the given cell array.

Now, in the code, if the "index" variable is not empty, then the target string will be found in the cell array. Therefore, the output of the program will be "The target string was found at index 4.", indicating that the target string is present at index 4 in the given cell array.

Method 2: Using ismember() and find() Functions

In MATLAB, we can also use the combination of ismember() and find() functions to find an exact string in a cell array.

Example 2

The following MATLAB program demonstrates how to find an exact string in a cell array using the combination of ismember() and find() functions in MATLAB.

% Create a cell array of strings
A = {'Tutorials', 'Point', 'for', 'MATLAB'}
% Choose a target string
target_string = 'MATLAB'
ismember(A, target_string)
% Check if the target string is in the cell array
index = find(ismember(A, target_string))
% Check if the target string is found
if ~isempty(index)
   fprintf('The target string is found at index %d.
', index) else fprintf('The target string is not found.
') end

Output

It will produce the following output

A = 1×4 cell array
{'Tutorials'}    {'Point'}    {'for'}    {'MATLAB'}
target = 'MATLAB'
ans = 1×4 logical array
0   0   0   1
index = 4
The target string is found at index 4

Explanation

In this MATLAB code, we have used the ismember() function to check whether the target string is in the cell array. In MATLAB, the "ismember()" function returns a logical array where each element is true (logic 1) when the corresponding element of the cell array is equal to the target string.

We have used the find() function to find the index of the true (logic 1) element in the logical array that corresponds to the index of the target string in the given cell array. Finally, if the "index" variable is not empty, the target string will be found in the cell array.

Hence, the output of the MATLAB program will be "The target string is found at index 4." This shows that the target string is found at the index 4 in the given cell array.

Conclusion

We can use a combination of "strcmp()" and "find()" or "ismember()" or "find()" functions to find an exact string in a cell array in MATLAB. Both "strcmp()" and "ismember()" functions return a logical array in which each element is true (logic 1) if the corresponding element of the cell array is equal to the target string. Therefore, using these two MATLAB functions, we can easily and quickly search for a specific element withing a cell array and can obtain its index value in the cell array.

Updated on: 26-Jul-2023

231 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements