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 commentaires

Adam Danz
Adam Danz le 19 Juil 2022
Why do you need to remove the data from the neatly organized structure (load_mat)?
Just use load_mat.DEF instead of new_var.

Connectez-vous pour commenter.

 Réponse acceptée

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 commentaires

alphabetagamma
alphabetagamma le 19 Juil 2022
This is very helpful. Thank you
Voss
Voss le 19 Juil 2022
You're welcome!
Actually, I have one last follow-up question. Let's say that from the new_mat structure, I want to only save the first two doubles, how I do that? More generally, if there are n doubles, and I would like to store( n-p) doubles, then what would be a good approach? I tried the following but it didn't work:
Perhaps it works on for other types such as in matrices.
new_mat1 = new_mat(1:end -1)
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}
N_keep = 2; % keep the first at-most-2 20x1 doubles
keep_count = 0;
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});
keep_count = keep_count+1;
end
if keep_count == N_keep
break
end
end
new_mat
new_mat = struct with fields:
ABC: [20×1 double] DEF: [20×1 double]
Note that the order of the fields will be as returned by fieldnames.
alphabetagamma
alphabetagamma le 21 Juil 2022
Sorry I forgot to reply earlier. Thanks a ton again :)
Voss
Voss le 21 Juil 2022
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by