필터 지우기
필터 지우기

search for substring in cell array of strings

조회 수: 1 (최근 30일)
monkey_matlab
monkey_matlab 2015년 6월 3일
댓글: monkey_matlab 2015년 6월 3일
Hello,
I have this cell array of strings:
A = { 'This is how we do the shuffle', 'All night long there', 'In the club'};
I want to return the cell index where there is no 'the' in the cell array. I am using the following code:
str = 'the';
the _str = sprintf('\\<%s\\>',str);
index = strfind(A,the_str);
index_nothe = find(cellfun(@isempty,index));
however, I get an empty array for index. I put the '\\<%s\\>' to search for only the word 'the'.
I would like to return [2] for index, as that is the only cell that does not contain 'the'.
Can you tell me where I am going wrong?
Thanks.

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2015년 6월 3일
편집: Azzi Abdelmalek 2015년 6월 3일
A = { 'This is how we do the shuffle', 'All night long there', 'In the club'}
idx=find(cellfun(@isempty,regexp(A,'the(\s)+','match')))
  댓글 수: 1
monkey_matlab
monkey_matlab 2015년 6월 3일
Hello, Thanks for your response Azzi!
I actually put the sprintf('\\<%s\\>',str) command because str is a variable and will change depending on varying inputs. That is, str will not always be 'the', it may be someting else.
But can you tell me why my attempt is not giving me the desired result?
Thanks.

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

카테고리

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