フィルターのクリア

Find index of cells containing a string

5 ビュー (過去 30 日間)
wesso Dadoyan
wesso Dadoyan 2016 年 8 月 15 日
コメント済み: wesso Dadoyan 2016 年 8 月 15 日
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.

採用された回答

Guillaume
Guillaume 2016 年 8 月 15 日
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 件のコメント
wesso Dadoyan
wesso Dadoyan 2016 年 8 月 15 日
Thanks for your very thorough reply.it works well now.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by