How to use structure variable names in for loop
Mostrar comentarios más antiguos
Hello,
I have a .mat file from a source containg structures with variable names as A,B,C etc.
I'm loading those to my workspace,creating a array for variable names and want to access the structures in a for loop. Error Dot indexing is not supported for variables of this type.
----------------------------------------------------------------------------
Structure names={'A','B','C'}
for i=1:3
output= Structure names(i).fieldname
end
5 comentarios
Tommy
el 28 de Abr. de 2020
What is your struct named? The below code shows how you can loop through each field in a struct called S:
names = fieldnames(S);
for i = 1:numel(names)
disp(S.(names{i}))
end
jinang patel
el 28 de Abr. de 2020
Tommy
el 28 de Abr. de 2020
Oh I see. Instead of storing the names of the structures in a cell array, can you store the structures themselves?
Structure={A,B,C,..,n};
for i=1:numel(Structure)
output= Structure{i}.fieldname;
end
jinang patel
el 28 de Abr. de 2020
Tommy
el 28 de Abr. de 2020
Fantastic! Trying to break a habit of answering questions in the comments...
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Structures en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!