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

2 ビュー (過去 30 日間)
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.

採用された回答

Walter Roberson
Walter Roberson 2022 年 9 月 21 日
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 件のコメント
Walter Roberson
Walter Roberson 2022 年 9 月 21 日
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 2022 年 9 月 21 日
Hi Walter, it works. Thanks a lot.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by