필터 지우기
필터 지우기

Loop over fieldnames in a matlab structure

조회 수: 183 (최근 30일)
David Stevens
David Stevens 2015년 8월 11일
편집: Bill Tubbs 2023년 6월 21일
Dears,
I have a MatLab "struct", with different "level" and "sub-structures". When printed to a cell, the data contained in the "struct", look like that:
report.COUNTRY.SOURCE.SCENARIO.CATEGORY.ENTITY = YEAR YEAR ...;
In order to produce tables listing the different variables combinations, I would like to loop over the fieldnames contained within the "struct".
I am currently trying to write a function able to do that:
fields=fieldnames(struct);
for categoryidx=1:length(fields)
categoryname=fields{categoryidx};
if isstruct(struct.(categoryname))
category=fieldnames(struct.(categoryname));
for entityidx = 1:length(category);
entityname = category{entityidx};
if isstruct(struct.(categoryname).(entityname))
gases=fieldnames(struct.(categoryname).(entityname));
end
end
end
end
Unfortunately, this is just producing anything! Does anyone has any idea how to loop over fieldnames in such a matlab structure? Thank you!

답변 (3개)

Titus Edelhofer
Titus Edelhofer 2015년 8월 11일
Hi David,
generally speaking you seem to know how to loop on fieldnames ... I'm not sure now where your problem is? You can change your function to work recursively, if you don't know the number of levels, something like (note, "struct" is not an ideal variable name)
function aTable = loopovernestedstructs(aTable, aStruct)
fields = fieldnames(aStruct);
for idx = 1:length(fields)
aField = aStruct.(fields{idx});
if isstruct(aField)
aTable = loopovernestedstructs(aTable, aField);
else
% add to the table what you want, e.g., if it's a number:
aTable(end+1) = aField;
end
end
Hope this helps,
Titus
  댓글 수: 2
David Stevens
David Stevens 2015년 8월 12일
Thank you very much for your answer! I have change the name of "struct" to "myStruct". I don´t really need to implement a recursive function as I already know the number of structure level (5). Then the aim of my function is to write a table containing variables of level 4 as a column header, the variables of level 5 as a row header, and then filled by existing results combination between level 4 and 5. As an example when I print the struct the output looks like that:
report.COUNTRY.SOURCE.SCENARIO.CATEGORY.ENTITY=YEAR YEAR
It could be great if you have some other hints! Thank you.
Titus Edelhofer
Titus Edelhofer 2015년 8월 13일
편집: Titus Edelhofer 2015년 8월 13일

Hi David,

maybe you can give a simple example, how it looks like, e.g.

report.USA.Reut.Scen01.Metal.Company01 = [2001 2002];
report.USA.Reut.Scen01.Metal.Company02 = [2003 2004 2005];
report.USA.Reut.Scen01.Rail.RailCompany01 = [2015];

and then how your table should look like ... ?

Then we should be able to help you out ...

Titus

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


Walter Roberson
Walter Roberson 2015년 8월 11일
That appears to be me to broadly correct to me, just incomplete as it stops analyzing at gases and does not do anything with what it finds.
Have you considered using a recursive routine? Hand it a variable and the "left" information to print out. If it detects the variable is a struct, iterate over its fields, passing the field content and the "left" information and the field name recursively into the routine. If it detects that it is working with a non-struct, print or return a report on the content.
  댓글 수: 1
David Stevens
David Stevens 2015년 8월 12일
Hi, Thank you for your answer. It´s not really necessary for me to do a recursive routine as I already know the number of structure level (5). As you suggested, I would like to print the results in a table. For this table I would like to have the level 4 of the structure as the column header and the number 5 of the structure as the row header. Then the table shall be filled with the existing combination results between level 5 and 4. Do you have any idea about how to achieve that?

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


Bill Tubbs
Bill Tubbs 2023년 2월 20일
편집: Bill Tubbs 2023년 2월 20일
There is another way to loop over the fieldnames:
for field = fieldnames(mystruct)'
if isstruct(mystruct.(field{:}))
...
end
end
The only annoying thing I find is that field is a cell not a string so to get the value you have to use the {:} notation. NOTE: It is important to transpose the fieldnames into a row vector.
I found one way to avoid this but there might be better ways:
for field = string(fieldnames(mystruct))'
if isstruct(mystruct.(field{:}))
...
end
end
  댓글 수: 2
Grigorii Nefedov
Grigorii Nefedov 2023년 6월 21일
even
for field = string(fieldnames(mystruct))'
if isstruct( mystruct.(field) )
...
end
end
Bill Tubbs
Bill Tubbs 2023년 6월 21일
편집: Bill Tubbs 2023년 6월 21일
Yes, sorry, that is what I meant. Should I update my comment now that you have pointed this out?

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by