필터 지우기
필터 지우기

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 CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by