searching cell arrays

I want to find the index of a cell array that contains a specific string, but the search and find functions don't take cell arrays as arguments. How do I return the cell number that contains the string?

 채택된 답변

Walter Roberson
Walter Roberson 2012년 2월 24일

1 개 추천

Should the cell exactly match the specific string? If so then use ismember().
Should the cell member start with the specific string? If so then cellfun() and strncmp()
Should the cell member contain the specific string as a substring somewhere in it? If so then cellfun() and strfind() and isempty()
The matching process on a cellstr can also be done by regexp(); you might need to cellfun() isempty() to determine the logical result for each entry. It is, though, well suited to returning substring locations, and if you have time it would be worth reviewing regexp() as a serious possibility.

댓글 수: 1

Jan
Jan 2012년 2월 24일
STRNCMP operates on cell strings directly, so you do not need CELLFUN.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Jan
Jan 2012년 2월 24일

1 개 추천

C = {'abc', 'hello', '81'}
strcmp(C, 'hello')
% >> 0 1 0
find(strcmpi(C, 'HELLO'))
% >> 2
the cyclist
the cyclist 2012년 2월 24일

1 개 추천

Here's one way, similar to Jan's:
C = {'abc', 'hello', '81'};
find(ismember(C,'hello'))

카테고리

도움말 센터File 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