I'm trying to find a list of row indices at which a specific string appears in a cell array. For example, for the following cell array:
input = {[546] 'Blah blah blah'
[783] 'Magic string'
[1341] 'Blah, blah, other stuff'
[1455] 'Magic string'
[1544] 'Another irrelevant string'
[1700] 'Yet another pointless string'
[1890] 'Magic string'}
...If I wanted to find the string 'Magic string'
, and return the row indices of these, I would hope to end up with a vector:
output =
2 4 7
... Because the 'Magic string'
exists on the 2nd, 4th and 7th rows of the input
.
I have tried doing the following:
output = strfind({input{:,2}},'Magic string');
output = cell2mat(output);
output = find(output);
However, this fails at the second line, because when the cell2mat()
operation is run, it removes all the blanks, rather than returning NaN
for these cells. I can't run an isnan()
operation on a cell array, so how could I acheive this?
For input
input = {[546] 'Blah blah blah'
[783] 'Magic string'
[1341] 'Blah, blah, other stuff'
[1455] 'Magic string'
[1544] 'Another irrelevant string'
[1700] 'Yet another pointless string'
[1890] 'Magic string'}
just use
output = find(strcmp(input(:,2), 'Magic string'));
Or, if strings can appear in any column,
output = find(any(strcmp(input, 'Magic string'), 2));
The key is that strcmp
can be applied to cell arrays containing not only strings. For cells that contain anything other than a string it simply returns 0
.