How to append structure fields with same name and different length

7 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Stephen23
Stephen23 am 2 Mai 2019
Bearbeitet: Stephen23 am 2 Mai 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

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by