Save into cell or ND array
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Hello Matlabers
I have the following code.
m = 2;
n = 3;
r = 5;
V = 2:2:20;
for ii = 1:5
W = rand(m,r);
H = rand(r,n);
S = struct();
for jj = 1:numel(V)
S(jj).W0 = rand(m,V(jj));
S(jj).H0 = rand(V(jj),n);
S(jj).rank = V(jj);
end
F = fullfile('DATA',sprintf('data_%d.mat',ii));
save(F,'W','H','S')
end
The above code saves the files as
Data1.mat
W, H and S % where S is a structure containing all values of the inner loop W0, H0
Data2.mat
W, H and S
.
.
Data5.mat
W, H and S
I dont want W0 and H0 to be in a struct. I want them to be saved individually like W and H because I will be using them as input to a function later. So ideally this is how i want to save it
% if i open the matfile i want to see the individual variables.
% and not being group into 1 struct. I want control over each of them.
Data1.mat
W,H
W0_2, W0_4, W0_6, ..., W0_20
Data_2.mat
W,H
W0_2, W0_4, W0_6, ..., W0_20
.
.
Data_5.mat
W,H
W0_2,W0_4,W0_6,...,W0_20
Thanks
2 Commenti
Risposta accettata
Guillaume
il 10 Gen 2019
Numbered variables are always a bad idea. You won't be able to loop over them and referring to them in any generic way will force you to use slow constructs (eval) which will make debugging painful and make the code more obscure.
Much better, use indexing which is what matlab is good at. Your question title asks about storing the data into a cell array which is the way to go. Since the various W have different sizes, a ND array is not an option. To store into a cell array, well use that instead of a struct (or convert the structure to a cell array afterward, but that's a bit roundabout):
for ii = 1:5
W = rand(m,r);
H = rand(r,n);
W0 = cell(size(V));
H0 = cell(size(V));
for jj = 1:numel(V)
W0{jj} = rand(m,V(jj));
H0{jj} = rand(V(jj),n);
%rank is V anyway, so save V
end
F = fullfile('DATA',sprintf('data_%d.mat',ii));
save(F,'W','H','W0', 'H0', 'V')
end
4 Commenti
Guillaume
il 11 Gen 2019
what am i doing wrongly
My code uses:
matcontent = load(...
I'm using load with an output. That means the content of the mat file is loaded into the structure matcontent (where each variable in the mat file ends up as a field of the structure). Therefore, the variable Q becomes matcontent.Q, etc.
This is a lot safer than the way you're using load, without an output, which just poofs variables into existance, possibly overwriting existing ones.
In addition, you're using matcontent as the filename, so I'm not sure what you're expecting out of matcontent.Winit. Indeed a char variable does not support dot indexing. It's not a structure. And even if matcontent was the structure containing the content of the file as I intended, you don't have a Winit variable in your list of variables in the load call.
The correct version of your function should probably be:
function [] = runs(sNR,Mv,i,Tmax,passes)
filename = fullfile('DATA', sprintf('data_%d_%d_%d.mat',sNR,Mv,i));
matcontent = load(filename); %load all variables into the structure
for combidx = 1:numel(matcontent.W0) %try every combination
[~,~,RRE_NMF,T_NMF]=NMF(matcontent.X, matcontent.Q, matcontent.W0{combidx}, matcontent.H0{combidx}, Tmax, passes);
save(sprintf('output/NMF_%d_%d_%d_%d_%d.mat', sNR, 100*Mv, matcontent.rank, i, combidx), 'RRE_NMF', 'T_NMF', '-v7.3')
end
end
Note that I've added combidx to the output filename as your original function would overwrite the same output file for each combidx.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Structures in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!