getfield to get values from an entire struct array?
37 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ken
el 29 de Mzo. de 2024
Comentada: Ken
el 30 de Mzo. de 2024
I have an array of objects of type Peak. Given the name of a field (I will be using several in turn), I would like to get the value of that field for every object in the array, resulting in a vector of double. Is there any way to ask getfield to do that, without resorting to a for loop? Or is there some related function that would do the job for me?
Respuesta aceptada
Bruno Luong
el 29 de Mzo. de 2024
Create 1 x 3 struct array with field named 'afield'
s = struct('afield', {11 12 13})
s(1)
s(2)
s(3)
% Here is the command that might be usefule for you
A = cat(2, s.afield)
3 comentarios
Bruno Luong
el 29 de Mzo. de 2024
Editada: Bruno Luong
el 29 de Mzo. de 2024
Take a look at dynamic fieldname
s = struct('afield', {11 12 13});
fieldname = "afield"
A = cat(2, s.(fieldname))
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!