Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
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.

Réponse acceptée

Walter Roberson
Walter Roberson le 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 commentaires
Walter Roberson
Walter Roberson le 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
sam
sam le 21 Sep 2022
Hi Walter, it works. Thanks a lot.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by