Search code examples
arraysstringmatlabcell-array

Get indices of string occurrences in cell-array


I have a cell array that contains a long list of strings. Most of the strings are in duplicates. I need the indices of instances of a string within the cell array.

I tried the following:

[bool,ind] = ismember(string,var);

Which consistently returns scalar ind while there are clearly more than one index for which the contents in the cell array matches string.

How can I have a list of indices that points to the locations in the cell array that contains string?


Solution

  • As an alternative to Divakar's comment, you could use strcmp. This works even if some cell doesn't contain a string:

    >> strcmp('aaa', {'aaa', 'bb', 'aaa', 'c', 25, [1 2 3]})
    ans =
         1     0     1     0     0     0