Filter löschen
Filter löschen

Find index of cells containing a string

5 Ansichten (letzte 30 Tage)
wesso Dadoyan
wesso Dadoyan am 15 Aug. 2016
Kommentiert: wesso Dadoyan am 15 Aug. 2016
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.

Akzeptierte Antwort

Guillaume
Guillaume am 15 Aug. 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
  1 Kommentar
wesso Dadoyan
wesso Dadoyan am 15 Aug. 2016
Thanks for your very thorough reply.it works well now.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by