How to loop over a structure in matlab
371 views (last 30 days)
Show older comments
Hi, I am new to matlab and learning it. I have a 1(1X1) main struct which again has 5(1X1) nested structs inside it which has 3(1X100) tables and I would like to finally fetch the value of those 3 tables but not sure how. Please help
3 Comments
Stephen23
on 27 Feb 2023
Edited: Stephen23
on 28 Feb 2023
@Learn Matlab: Please upload some representative data in a MAT file by clicking the paperclip button. It does not have to be your top-secret data, just something with exactly the same arrangement. Data descriptions are rarely correct.
Accepted Answer
Stephen23
on 27 Feb 2023
Edited: Stephen23
on 27 Feb 2023
As far as I can tell, this is your data structure (for simplicity I will define only two field Names, but the code works for any number of Names). I will assume that you really do mean table-type when you write the term "table":
S.Name1.Table1 = array2table(rand(7,5));
S.Name1.Table2 = array2table(rand(7,5));
S.Name1.Table3 = array2table(rand(7,5));
S.Name2.Table1 = array2table(rand(7,5));
S.Name2.Table2 = array2table(rand(7,5));
S.Name2.Table3 = array2table(rand(7,5));
I will assume that your goal is to vertically concatenate the tables together (even though I asked for clarification here, so far you have not specified this), and that you want them concatenated in the same order as the fields have. I will also assume that the tables have compatible sizes, types, column/variable names, etc.
C = struct2cell(S);
T = [C{:}]
T1 = vertcat(T.Table1)
T2 = vertcat(T.Table2)
T3 = vertcat(T.Table3)
This code uses comma-separated lists:
2 Comments
Stephen23
on 28 Feb 2023
Edited: Stephen23
on 28 Feb 2023
"a way we could get values for each names lets say we would have TN1 table with only Name1's data then another table TN2 with Name2's data separate?"
Of course, just replace VERTCAT with a cell constructor:
C1 = {T.Table1}
C2 = {T.Table2}
C3 = {T.Table3}
Then you can access the data using basic, efficient indexing:
For example the 2nd Name of Table1:
C1{2}
Your request is confusing because earlier you wrote that you want "... to store those 3 final tables separately and not planning on giving them unique variable names... not planning on giving them unique variable names", i.e. that you expected exactly three output tables. Now you are apparently asking for exactly that which you stated you were not asking for. Changing what you request makes it harder to help you.
Do NOT force yourself into writing slow, complex, inefficient, buggy code by using dynamically-named variables:
More Answers (1)
Cameron
on 27 Feb 2023
Depends on how your data is placed in the function. Like @Walter Roberson said, structfun works well if your data looks like this
T.S.X = rand(1,1000);
T.S.Y = rand(1,1000)*2;
T.S.Z = rand(1,1000)*3;
p = structfun(@median,T.S);
disp(p)
Another way to do it is to loop through them.
T.S.X{1} = rand(1,1000);
T.S.X{2} = rand(1,1000)*2;
T.S.X{3} = rand(1,1000)*3;
for xx = 1:length(T.S.X)
disp(median(T.S.X{xx}))
end
6 Comments
Walter Roberson
on 28 Feb 2023
MainData --> Name1 --> Table 1(1x100), Table 2(1x100), Table 3(1x100)
Okay, that is 3 tables. Suppose you store those into variables named Table1, Table2 and Table3
MainData --> Name2 --> Table 1(1x100), Table 2(1x100), Table 3(1x100)
okay, that is 3 more tables. Do you want to now overwrite variables named Table1, Table2 and Table3 or do you want them stored into a different variable name?
If you were wanting to store to Table1{1} for Name1 and Table1{2} for Name2 and so on, using a cell array with one entry per field, then that is relatively easy. But if you want to store into (for example) Name1_Table1 and Name2_Table1 and so on, with the variable name depending on the field name, then the code gets uglier.
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!