Automatically put content of structure fields in a matrix
1 次查看(过去 30 天)
显示 更早的评论
Hi all,
For the following structure:
A.TEST=rand(15x8);
A.TEST1=rand(15x8);
A.TEST2=rand(15x8);
A.TEST3=rand(15x8);
A.TEST4=rand(15x8);
A.TEST5=rand(15x8);
I would like to find an optimal way to create a new matrix (let's say B) that contains all values from TEST to TEST5 concatenated vertically, which would be B = (15*5,8).
Preferabally automatically, since my real dataset is much bigger.
Thanks!
2 个评论
采纳的回答
darova
2020-5-4
编辑:darova
2020-5-4
Try getfield
fnames = fieldnames(A);
B = [];
for i = 1:length(fnames)
b1 = getfield(A,fnames{i});
B = [B; b1];
end
Use preallocation if for loop is slow
2 个评论
darova
2020-5-5
Use nested for loop
fnames = fieldnames(A);
B = [];
for i = 1:length(fnames)
b1 = getfield(A,fnames{i});
s = whos('b1');
if strcmp('struct',s.class) % if type of variable is 'struct'
fnames1 = fieldnames(b1);
for j = 1:length(fnames1)
b2 = getfield(b1,fnames1{j});
B = [B; b2];
end
else
B = [B; b1];
end
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!