Clear Filters
Clear Filters

How to extract specific struct data from multiple mat files? Please help. Thank you so much.

1 view (last 30 days)
I have the following 5 mat files for 5 samples of data type 0. These mat files contain lot of data structure and variables. I want to extract a specific struct type data named ap_struct from each these mat files using loop. How can I do that? Thank you so much.

Accepted Answer

Walter Roberson
Walter Roberson on 21 Sep 2022
N = 5;
ap_structs = cell(N,1);
for K = 1 : N
filename = K + "pbs0.mat";
datastruct = load(filename, 'ap_struct');
ap_structs{K} = datastruct.ap_struct;
end
%output is cell array ap_structs
Here, I do not assume that the ap_struct in the different files have exactly the same fields in exactly the same order.
If they do contain the same fields in the same order then instead you can use
N = 5;
clear ap_struct;
for K = N : -1 : 1
filename = K + "pbs0.mat";
datastruct = load(filename, 'ap_struct');
ap_struct(K) = datastruct.ap_struct;
end
%output is non-scalar struct ap_struct
  7 Comments
Walter Roberson
Walter Roberson on 21 Sep 2022
N = 5;
ap_struct = struct('times', {});
for K = 1 : N
filename = K + "pbs0.mat";
datastruct = load(filename, 'ap_struct');
ap_struct = [ap_struct, datastruct.ap_struct];
end

Sign in to comment.

More Answers (0)

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!