How to use structure variable names in for loop

Hello,
I have a .mat file from a source containg structures with variable names as A,B,C etc.
I'm loading those to my workspace,creating a array for variable names and want to access the structures in a for loop. Error Dot indexing is not supported for variables of this type.
----------------------------------------------------------------------------
Structure names={'A','B','C'}
for i=1:3
output= Structure names(i).fieldname
end

댓글 수: 5

What is your struct named? The below code shows how you can loop through each field in a struct called S:
names = fieldnames(S);
for i = 1:numel(names)
disp(S.(names{i}))
end
I have "n" structures, each with unique names. I want to access a particular fieldname in each structure. So I thought creating a array with unique structure names= {'A','B'.....'n'} and then
for i=1:n
output= structure names {i}.fieldname
but this is not possible since structure names {i} returns a character 'A'
Oh I see. Instead of storing the names of the structures in a cell array, can you store the structures themselves?
Structure={A,B,C,..,n};
for i=1:numel(Structure)
output= Structure{i}.fieldname;
end
Great that works!! Thanks
Tommy
Tommy 2020년 4월 28일
Fantastic! Trying to break a habit of answering questions in the comments...

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

 채택된 답변

Tommy
Tommy 2020년 4월 28일
Loop through the structures instead of their names:
Structure={A,B,C,..,n};
for i=1:numel(Structure)
output= Structure{i}.fieldname;
end

추가 답변 (0개)

카테고리

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

질문:

2020년 4월 28일

댓글:

2020년 4월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by