How to assign a struct array to a field of another struct array?
Mostrar comentarios más antiguos
Hello everybody, thanks in advance for your help with this!
So I'm trying to assign a struct array to a field of another struct array when both have the same size and obviously trying to avoid a for loop.
It looks something like this
data % This is a struct array that doesnt have the field trait yet
trait % this is a struct array of the same length of data
assert(length(data) == length(trait))
data.t = trait % this fails with --> Scalar structure required for this assignment.
[data.t] = [trait] % this fails with --> Insufficient number of outputs from right hand side of equal sign to satisfy assignment.
[data.t] = [trait] % this fails with --> Insufficient number of outputs from right hand side of equal sign to satisfy assignment.
[data.t] = deal(trait) % works but assigns the whole trait struct array to the field t in each element of data
Im pretty sure that if trait was a cell where each element of the cell was each of the elements in the struct array then this should work
[data.t] = cellTrain{:}
But I cant figure out a way to expand the trait struct-array into a cell as struct2cell expands every field in every struct of the struct array into cells.
Finally for completion the result should be equivalent to this simple for loop but hopefully a bit more efficient
for i = 1:length(data)
data(i).t = trait(i);
end
Thanks once again for all the help!
2 comentarios
madhan ravi
el 22 de Feb. de 2019
attach your struct as .mat file
Juan Sierra
el 22 de Feb. de 2019
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!