concatenate vector within nested structures
Show older comments
'DATA' is a 1x1 struct containing 217 fields. Each field is a structure containing a double array labelled 'data'.
I would like to concatenate the 'data' array in each of the 217 fields into one vector.
The structure containing the dat ais attached
any help would be greatly appreciated.
Answers (1)
David Hill
on 28 Oct 2021
z=[];
for k=1:217
s=sprintf('DATA.Segment%d.data',k);
z=[z;eval(s)];
end
2 Comments
Matthew Mitchell
on 28 Oct 2021
Akira Agata
on 1 Nov 2021
Edited: Akira Agata
on 1 Nov 2021
+1
The following way can avoid 'eval' function:
load("lvm2matDATA.mat");
% Extract field names containing "Segment"
list = fieldnames(DATA);
list = list(contains(list,"Segment"));
% Concatenate data
tData = [];
for kk = 1:numel(list)
tData = [tData, DATA.(list{kk}).data];
end
% Arrange it as a table variable
tData = array2table(tData,"VariableNames",list);
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!