Find Index of Cells Containing My String in MATLAB
Last Updated :
28 Apr, 2025
Cell Arrays in MATLAB are a type of array that store data in the form of cells. The benefit of using these cell arrays is that they can store data of different types as cells within a cell array only. In this article, we will see how to find a given string in a cell array.
Firstly, we shall check whether the given cell array contains all strings or not. This can be done by the following function:
iscellstr(<cell array>)
It will return 1 if it contains all strings else, 0.
Example 1:
Matlab
% MATLAB array
arr = {'geeks','for','geeks','geek'}
Output:
As it can be seen that all elements of the above array contain all strings so, we shall move ahead.
Now, we can find a string in two manners:
- Part of a string cell.
- Exact matches a string cell.
We shall see both these cases and how to do them.
Finding the Index of My String as Part of a Cell:
To find my string as pattern matching/part of the string, we can use the contains a () function which can then, be passed to the find() function to get the indices.
Syntax:
indices = find(contain(array,string))
Example 2:
Matlab
% MATLAB Array code
arr = {'geeks','for','geeks','geek'};
% Converting to cell array
arr=cell(arr);
%The string to be matches
str='gee';
% Getting indices
indices = find(contains(arr,str))
Output:
As it can be verified that the string 'gee' appears in cells 1, 3, and 4.
Finding an Index of My String as an Exact Cell:
To find exact match of my string we shall use the strcmp() function which returns a logical array for exact matches. Then by passing it to the find() function, we shall get the indices of my string's exact match.
str_logical = strcmp(array,my_string)
indices = find(str_logical)
See the following code for understanding the same.
Example 3:
Matlab
% MATLAB code for Finding an index of
% my string as an exact cell
% cell array
arr = {'geeks','for','geeks','geek'};
arr=cell(arr);
% Defining two strings for comparison
str1='geek';
str2='geeks';
% Making logical arrays for both strings
str1_log = strcmp(arr,str1);
str2_log = strcmp(arr,str2);
% Finding indices of exact match of both strings
indices_str1 = find(str1_log);
indices_str2 = find(str2_log);
Output:
As we can see, it matched the 'geek' string only to its exact match and not to 'geeks'.
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
Comparing Two Cell Arrays of Strings of Different Sizes in MATLAB MATLAB is a high-performance language that is used for matrix manipulation, performing technical computations, graph plottings, etc. It stands for Matrix Laboratory. Cell Array: A cell Array in MATLAB is a data type that can store any type of data. Data is stored in cells in a cell array. It is init
2 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 Find the Position of a Number in an Array in MATLAB? Finding the position of a number in an array, which can be done using the find() function. The find() function is used to find the indices and values of the specified nonzero elements. Syntaxfind(X) Parameters: This function accepts a parameter. X: This is the specified number whose position is goin
1 min read
How to remove space in a string in MATLAB? In this article, we are going to discuss how to remove space from a string in MATLAB with the help of isspace(), find(), strrep(), and regexprep() functions. Using isspace() The isspace() function is used to identify elements that are ASCII white spaces. isspace('string') is used to find the white s
6 min read
How to Find Indices and Values of Nonzero Elements in MATLAB? MATLAB provides functionality that finds the indices and values of all non-zero elements in a vector or multidimensional array; the find() function. The find function with required parameters gives back a vector containing indices of non-zero elements in the passed array/vector. Syntax: vec = [] %so
3 min read