0

I have a MatLab struct array as follow:

'country.source.scenario.category.entity= year'

I would like to loop over the existing 'country.source.scenario' combinations and produce cell or matrix containing the fields: category, entity and year. Anyone as an idea about how to do that? Thanks

3
  • 2
    please provide a short example of what your input looks like and what is your expected output Commented Aug 6, 2015 at 13:05
  • @Shai - My input is a tree if data going from "countries" to "sources" to "scenarios" to "categories" to "entities" ending with "years" contained in a <x*x double>. Basically I have all kind of combinations (e.g. USA.CRF.HISTORY.CAT0.CH4= 2000 2001 2002). From that i would like to lock the existing COUNTRY.SOURCE.SCENARIO combination (such as USA.CRF.HISTORY) and build tables (or whatever other outputs) containing CATEGORY and ENTITY as X and Y and filled with the corresponding years. Commented Aug 6, 2015 at 13:13
  • 2
    press "edit" above and edit details into your question. Commented Aug 6, 2015 at 13:14

1 Answer 1

1

You can use fieldnames to get the fields at each level, and isstruct at each sublevel to see if you need to keep drilling, e.g.

fields = fieldnames(str);
for field = fields'
    sub = str.(field{1});
    if isstruct(sub)
        %loop through fieldnames
    end
end

You can put it in a recursive function whereby inside the if you again call the function. I did not do it like that because it was not clear to me what you wanted as a result, it seemed like you only wanted the tags and not the values at the end.

Sign up to request clarification or add additional context in comments.

1 Comment

@A Hernandez Thank you for your input! I was just doing that way: for fieldidx=1:length(fields) fieldname=fields{fieldidx}; if isstruct(struct.(fieldname)) but at some point I have any idea about how to organize the loop. At the end I would like to have an output (table) listing for the CATEGORIES as an header row, and ENTITIES as an header line and filled with the YEAR of each CATEGORIES.ENTITIES combinations!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.