How to loop over a structure in matlab
472 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2023-2-27
编辑:Stephen23
2023-2-28
@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.
采纳的回答
Stephen23
2023-2-27
编辑:Stephen23
2023-2-27
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 个评论
Stephen23
2023-2-28
编辑:Stephen23
2023-2-28
"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:
更多回答(1 个)
Cameron
2023-2-27
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 个评论
Walter Roberson
2023-2-28
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!