Access data in two dimensional struct array
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have a struct array with two dimensions:
temp = struct('a',5,'b',7);
bla = repmat(temp,4,8);
I would now like to get the data from one field as a matrix:
data = [bla.a];
However, I now get a vector with all the entries in one dimension. I would like to keep the matrix dimensions, e.g. something like that:
data = zeros(size(bla));
for i=1:size(data,1)
for j=1:size(data,2)
data(i,j) = bla(i,j).a;
end
end
Is there a more elegant way to do this?
0 commentaires
Réponses (1)
Steven Lord
le 14 Juil 2021
There's no guarantee that the field in the elements of a struct array have the same type or size, so making an array may not make sense. The best you could do in general would be to make a cell array and reshape it. That same technique would work with a regular array if all the fields have scalars that can be concatenated, too.
s1 = repmat(struct('x', 1), [3 3]);
for k = 1:numel(s1)
s1(k).x = k+zeros(k);
end
A = {s1.x}
B = reshape(A, size(s1))
check = isequal(B{2, 3}, s1(2, 3).x)
0 commentaires
Voir également
Catégories
En savoir plus sur Structures dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!