Effacer les filtres
Effacer les filtres

Find index of cells containing a string

5 vues (au cours des 30 derniers jours)
wesso Dadoyan
wesso Dadoyan le 15 Août 2016
Commenté : wesso Dadoyan le 15 Août 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.

Réponse acceptée

Guillaume
Guillaume le 15 Août 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 commentaire
wesso Dadoyan
wesso Dadoyan le 15 Août 2016
Thanks for your very thorough reply.it works well now.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by