How to append structure fields with same name and different length
1 view (last 30 days)
Show older comments
Sindre Hodneland
on 2 May 2019
Commented: Sindre Hodneland
on 4 May 2019
I have 20 mat files named Data.mat. Each Data.mat file have the same structs and fieldnames:
Struct: Acceleration Fieldnames: x, y and z
Struct: AngularVelocity Fieldnames: x, y and z
How can I append all the Acceleration.x together and all the AngularVelocity.x together from all the 20 Data.mat files? All the files have different length in the fieldnames
Ive tried this for a day and starting to get a headache, thanks for any replies!
The sample files attached have more structs and fields, but I only need the ones stated above.
Accepted Answer
Stephen23
on 2 May 2019
Edited: Stephen23
on 2 May 2019
You did not explain how these (identically named) files are saved (i.e. what the folder structure is like), so I simply put your three sample files into subfolders of the current directory, named A, B, and C. Of course you can use dir or sprintf or whatever suits your folder structure:
I assumed that by "append" you meant concatenation into one array.
D = {'A','B','C'}; % subfolder names (you gave no info on this).
N = numel(D);
C = cell(2,N);
for k = 1:N
F = fullfile(D{k},'Data.mat');
T = load(F);
C{1,k} = T.Acceleration;
C{2,k} = T.AngularVelocity;
end
Acc = [C{1,:}];
Ang = [C{2,:}];
AccX = vertcat(Acc.x);
AngX = vertcat(Ang.x);
Giving:
>> size(AccX)
ans =
75188 1
>> size(AngX)
ans =
75187 1
More Answers (0)
See Also
Categories
Find more on Structures 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!