any easier way to find cell by string

조회 수: 1 (최근 30일)
Yu Li
Yu Li 2019년 5월 15일
댓글: James Tursa 2019년 5월 15일
Hi:
I have a cell array like below:
A{1}='test1a';
A{2}='test2a';
A{3}='test3a';
A{4}='test4a';
A{5}='test1b';
A{6}='test2b';
A{7}='test3b';
A{8}='test4b';
...
I want to find the location where 'test*a' are at, in this case, it should be 1,2,3,4
Yes, I can write a for-loop to read-justify cell in this array one by one, but I want know if there is any easier way to do this? such as
strfind(A,'test*a')
of course this is not available in Matlab
Thanks!
Yu

채택된 답변

James Tursa
James Tursa 2019년 5월 15일
편집: James Tursa 2019년 5월 15일
Assuming the strings all start with 'test', e.g.
find(cellfun(@(C)C(end)=='a',A))
  댓글 수: 3
Yu Li
Yu Li 2019년 5월 15일
However, I think your answer provide a solution.... I can operate with your answer twice and then solution is found:
a=find(cellfun(@(C)C(end)=='a',A));
b=find(cellfun(@(C)C(end)=='test',A));
intersect(a,b)
Thanks!
Yu
James Tursa
James Tursa 2019년 5월 15일
Well, I think you would need to do something like this instead:
find(cellfun(@(C)numel(C)>4 && strcmp(C(1:4),'test') && C(end)=='a',A))
But really what is needed is an Answer from someone who knows regexp( ) well (and that isn't me).

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by