Need to use cellfun for returning index of array
이전 댓글 표시
Hello
I need to find index for string in structures when compare with required string.
I use the following code
for i=1:numel(LD)
if strcmp('Lam1',LD(i).Name)
break;
end
end
where i = array index.
Here I will find the index for string residing in LD(:).Names
is their any easy way to find the index using cellfun
I tried like this cellfun(@strcmp,'Lam1',LD(:).Names)
should return the index, correct me I am worng.
댓글 수: 2
You forgot to explain the type of LD.Name: Are they strings or cell strings? And btw., is it LD.Name or LD.Names in plural? I avoid corresponding programming errors by the simple convention:
- All names in singular
- When it matters if a variable is a scalar entity or a list of objects: aMyObject and MyObjectList.
I do not think that this is nicer, but avoiding sources of bugs is more important, especially because useful programs tend to grow.
Thulasi Durai Durai Samy
2012년 7월 9일
채택된 답변
추가 답변 (2개)
F.
2012년 7월 9일
try this :
regexp( { LD(:).Name } , 'Lam1' , 'once )
it's to find the first occurance of Lam1 in the cell array { LD(:).Name }
Walter Roberson
2012년 7월 9일
편집: Walter Roberson
2012년 7월 9일
cellfun(@(S) strcmp('Lam1',S), {LD.Name} )
카테고리
도움말 센터 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!