Azzera filtri
Azzera filtri

Find index of cells containing a string

5 visualizzazioni (ultimi 30 giorni)
Hi ,
Attached the "text" cell array. I tried to find the index of cells that match the string 'CERT'. I tried many approaches but all failed to identify the index of the cell. For example
cellfind = @(string)(@(cell_contents)(strcmp(string,cell_contents)));
logical_cells = cellfun(cellfind('CERT'),text);
gave only zeros. Any help to find the index of the cell which is the 7th row and the 2nd column in this case would be much appreciated.

Risposta accettata

Guillaume
Guillaume il 15 Ago 2016
The problem is not with your search expression, which works fine, but the fact that the exact string 'CERT' is not present in your cell array.
>>double(s{7, 2})
ans =
67 69 82 84 160 160
Notice the two 160 characters at the end of the string ([67 69 82 84] are the character codes for CERT). In my version of matlab, char(160) renders as a blank space (depending on your locale it may render differently).
In fact, if you look through your whole matrix, there are a fair number of strings where the character 160 appears. It seems that it's the only character outside the ASCII range, as well:
cellfun(@(s) double(s(s>127)), text, 'UniformOutput', false) %show characters outside ASCII
Probably, the simplest thing is to remove these characters (which I assume you did not want in the first place):
text = cellfun(@(s) s(s<128), text, 'UniformOutput', false);
Your search expression will then work:
>>[row, col] = find(cellfun(@(s) strcmp(s, 'CERT'), text))
row =
7
col =
2

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by