Extracting a field name from a cell array in a structure

I am trying to extract a field from a cell array of varying sizes. For instance, if I have the following:
name1.name2.name3.name4{1.2}.name5
I want to locate name5, but the cell array is of an unknown and varying size and name5 could be in any cell. It could be:
name1.name2.name3.name4{1,10}.name5
name1.name2.name3.name4{1,8}.name5
name1.name2.name3.name4{1,1}.name5
How do I search for a known field name within a complicated structure such as this?
Thank you, Susie

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2011년 12월 16일
A = name1.name2.name3.name4;
idx = find(cellfun(@isstruct,A));
i2 = cellfun(@(x)any(strcmp(x,'name5')),cellfun(@fieldnames,A(idx),'un',0));
idx = idx(i2);
out = A(idx)

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

질문:

2011년 12월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by