How to append structure fields with same name and different length

2 次查看(过去 30 天)
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.

采纳的回答

Stephen23
Stephen23 2019-5-2
编辑:Stephen23 2019-5-2
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by