Iterating Through Multiple Structures in One Structure
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Spencer Ferris
el 13 de Jun. de 2021
Comentada: Walter Roberson
el 13 de Jun. de 2021
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 comentarios
Respuesta aceptada
Walter Roberson
el 13 de Jun. de 2021
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 comentarios
Walter Roberson
el 13 de Jun. de 2021
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Structures en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!