Iterating Through Multiple Structures in One Structure
2 次查看(过去 30 天)
显示 更早的评论
I have a 1x1 structure that has 12 fields in it, which are all structures of varying sizes that all have the exact same fields inside of them.
What I want to do is iterate through all 12 structures in a for loop and do the same things to each of them. How would I do this? Do the field names have a "index" value or something similar that I can use to iterate through them? I haven't been able to find an answer online.
I also have all 12 structures seperate from each other, so if I need to iterate through them seperately I could do that, it seemed like the best way to do it was to put them all into one structure.
0 个评论
采纳的回答
Walter Roberson
2021-6-13
A = struct();
for K = 1 : 4
ts = struct('pqr', randi(9, 1, 3), 'stu', randi(9, 1, 3))
n = char(randi(0+['a', 'z'], 1, 5));
A.(n) = ts;
end
A
structfun(@(S) [mean(S.pqr), sum(S.stu)], A, 'uniform', 0)
2 个评论
Walter Roberson
2021-6-13
If you are invoking plot() from within the structfun() then unless you are careful to create a new axes or figure each time, then the actual lines are likely to be deleted as soon as the next plot starts (unless you had "hold on").
I recommend making what you structfun() into a real function (not anonymous) that creates a figure and axes, plot()'s, creates appropriate annotations and legends, and print() or saveas() or exportgraphics().. and then deletes the figure.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!