search string in array in matlab

Assuming you have an array of strings, you can use the strcmp function to find the index of a specific string in the array. Here's an example:

main.m
% create an array of strings
myArray = {'apple', 'banana', 'cherry', 'date'};

% search for the string 'banana'
idx = find(strcmp(myArray, 'banana'));

% print the index of the string 'banana'
disp(idx);
204 chars
9 lines

This code will output 2, which is the index of 'banana' in the myArray array. If the string you're searching for is not in the array, the idx variable will be empty (i.e. []).

related categories

gistlibby LogSnag