How to search for a string in cell array in MATLAB?

Let's say I have the cell array

strs = {'HA' 'KU' 'LA' 'MA' 'TATA'}

What should I do if I want to find the index of 'KU'?


Solution 1:

I guess the following code could do the trick:

strs = {'HA' 'KU' 'LA' 'MA' 'TATA'}
ind=find(ismember(strs,'KU'))

This returns

ans = 
     2

Solution 2:

>> strs = {'HA' 'KU' 'LA' 'MA' 'TATA'};
>> tic; ind=find(ismember(strs,'KU')); toc

Elapsed time is 0.001976 seconds.

>> tic; find(strcmp('KU', strs)); toc

Elapsed time is 0.000014 seconds.

SO, clearly strcmp('KU', strs) takes much lesser time than ismember(strs,'KU')