Access data in two dimensional struct array
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Steven Lord
am 14 Jul. 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 Kommentare
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!