Find Index of Cells Containing My String in MATLAB


In MATLAB, a cell array is a type of data structure used to hold data of different data types and sizes. In short, a cell array allows us to store different types of data like numbers, characters, strings, arrays, and more.

In a cell array, each cell can contain a specific type of data. Cell array is a powerful tool to handle heterogeneous data.

In MATLAB, the curly braces '{}' are used to create a cell array, i.e.

CellArray = {10, 'TutorialsPoint', [2 3 4]};

Hence, cell arrays play a vital role, when we need to store different types of data together or store data different sizes together.

As in this article, our main aim is to find the index of a specific string in a cell array using MATLAB. We can do this by using any of the following two methods:

  • By using a combination of loops and conditional statements

  • By using built-in functions

Let us now discuss each method in detail with the help of example programs.

Find the Index of My String using Loops and Conditional Statements

In MATLAB, we can use conditional statements and loops to find the index of cells containing my strings.

Example

The following MATLAB program demonstrates the implementation of code to find the index of cell containing my string.

% MATLAB program to find index of my string using loops and conditional statements
% Create a sample cell array of strings
A = {'Tutorials', 'Point', 'is', 'a', 'Great', 'Platform', 'to', 'Learn', 'Programming'};

% Specifying my string whose index I want to know
S = 'Learn';

% Create an empty array to store the index
I = [];

% Loop through the cell array
for i = 1:numel(A)
    if strcmp(A{i}, S)
        I = [I, i];
    end
end

% Display the index of cells containing my string
disp('Index of cells containing my string:');
disp(I);

Output

Index of cells containing my string:
     8

Explanation

In this MATLAB code, we start by creating a cell array 'A' which contains various strings. Then, we specify the string 'S' whose index we want to know in the cell array, as in this case 'Learn', you can change it as per your requirements.

After that we create an empty array 'I' to store the index of the cell containing the specified string. Next, we iterate a 'for' loop through the cell array, where we have used the 'strcmp' function to compare the cell string with the specified string. If the two strings match, the index of the cell containing the string will be stored to the index array 'I'.

Finally, we display the index of the cell containing the specified string by using the 'disp' function.

It is important to note that the loop method is not much efficient when the cell array has a large data set. In that case, we use some built-in functions to find the index of the specified string in the cell array.

Now, let us perform the same operation using a combination of built-in functions.

Find the Index of My String using Built-in Functions

In MATLAB, we can use a combination of built-in function to find the index of cell containing a specified string. Some commonly used combinations of built-in function to find the index of a cell are explained below with the help of example programs.

Example

The following MATLAB code shows how to find the index of my string using "find" and "strcmp" function:

% Create a sample cell array of strings
A = {'Tutorials', 'Point', 'is', 'a', 'Great', 'Platform', 'to', 'Learn', 'Programming'};

% Specifying my string whose index I want to know
S = 'Learn';

% Find index of cell containing the specified string
I = find(strcmp(A, S));

% Display the index of cells containing my string
disp('Index of cells containing my string:');
disp(I);

Output

Index of cells containing my string:
     8

Explanation

In this MATLAB code, we start by creating a sample cell array 'A' containing various strings. Next, we specify the string 'S' whose index in the cell array has to be found. After that, we use a combination of functions 'find' and 'strcmp' to find the index of the cell containing the specified string, which is in this example, 'Learn'. Finally, we display the result using the 'disp' function.

Example

The following MATLAB program shows how to find the index of my string using "find" and "contains" functions:

% MATLAB program to find index of my string using 'find' and 'contains' functions
% Create a sample cell array of strings
A = {'Tutorials', 'Point', 'is', 'a', 'Great', 'Platform', 'to', 'Learn', 'Programming'};

% Specifying my string whose index I want to know
S = 'Learn';

% Find index of cell containing the specified string
I = find(contains(A, S));

% Display the index of cells containing my string
disp('Index of cells containing my string:');
disp(I);

Output

Index of cells containing my string:
     8

Explanation

The implementation and execution of this MATLAB code is the same as that of the previous one. The only difference is that in this code we have used the 'contains' function instead of the 'strcmp' function to compare the specified string with the strings of the cell array.

Example

Now, let us consider another example program to find the index of multiple cells containing my strings.

% MATLAB program to find index of cells containing my strings
% Create a sample cell array of strings
A = {'Tutorials', 'Point', 'is', 'a', 'Great', 'Platform', 'to', 'Learn', 'Programming'};

% Specifying my string whose index I want to know
S1 = 'Tutorials';
S2 = 'Platform';
S3 = 'Programming';

% Find indices of cells containing the specified strings
I1 = find(strcmp(A, S1));
I2 = find(strcmp(A, S2));
I3 = find(strcmp(A, S3));

% Display the indices of cells containing my strings
disp('Index of cell containing Tutorials:');
disp(I1);
disp('Index of cell containing Platform:');
disp(I2);
disp('Index of cell containing Programming:');
disp(I3);

Output

Index of cell containing Tutorials:
     1

Index of cell containing Platform:
     6

Index of cell containing Programming:
     9

Conclusion

In this tutorial, we explained the concept how to find the index of cells containing a specified string in a cell array and demonstrated the practical code implementation with the help of examples.

Updated on: 07-Sep-2023

57 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements