For loop to save doubles in .mat files

4 ビュー (過去 30 日間)
alphabetagamma
alphabetagamma 2022 年 7 月 19 日
コメント済み: Voss 2022 年 7 月 21 日
I have a .mat file which stores the variables:
E.g: name: ABC, type : 20 x 1; class: double
name: DEF, type : 20 x 1; class: double
name: GHI, type : 20 x 1; class: double
name: JKL, type: 1 x 1; class: struct
name: JKL, type: 1 x 1; class: struct
name: XYZ, type: 7 x 1; class: cell
name: LMN, type : 30 x 1; class: double
etc.
I would like to use only the (20 X1) doubles one by one in another line of code. For instance,
load_mat = load('some_mat_file.mat')
new_var = load_mat.DEF;
new_var = load_mat.GHI;
But instead of writing repeatedly, could you provide an example of a for loop or something else? Thanks a lot in advance.
  2 件のコメント
Adam Danz
Adam Danz 2022 年 7 月 19 日
Why do you need to remove the data from the neatly organized structure (load_mat)?
Just use load_mat.DEF instead of new_var.
Stephen23
Stephen23 2022 年 7 月 19 日
編集済み: Stephen23 2022 年 7 月 19 日

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

採用された回答

Voss
Voss 2022 年 7 月 19 日
load_mat = load('some_mat_file.mat')
load_mat = struct with fields:
ABC: [20×1 double] DEF: [20×1 double] GHI: [20×1 double] JKL: [1×1 struct] LMN: [30×1 double] XYZ: {7×1 cell}
% create a new structure, "new_mat", which contains
% only the 20x1 doubles from load_mat:
new_mat = struct();
f = fieldnames(load_mat);
for ii = 1:numel(f)
if isa(load_mat.(f{ii}),'double') && isequal(size(load_mat.(f{ii})),[20 1])
new_mat.(f{ii}) = load_mat.(f{ii});
end
end
new_mat
new_mat = struct with fields:
ABC: [20×1 double] DEF: [20×1 double] GHI: [20×1 double]
  6 件のコメント
alphabetagamma
alphabetagamma 2022 年 7 月 21 日
Sorry I forgot to reply earlier. Thanks a ton again :)
Voss
Voss 2022 年 7 月 21 日
You're welcome!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by