Save into cell or ND array

1 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2019-1-10
编辑:Stephen23 2019-1-10
Original question and answer:
@fadams18: you have copied my code, so please accept my answer. Accepting answers is how you can show your appreciation for the volunteers who help you.
fadams18
fadams18 2019-1-10
I did accept it Bro. Thanks alot. Im under a deadline thats why i asked a new question.

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2019-1-10
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 个评论
fadams18
fadams18 2019-1-10
I applied the idea to my program
function [] = runs(sNR,Mv,i,Tmax,passes)
matcontent = fullfile('DATA', sprintf('data_%d_%d_%d.mat',sNR,Mv,i));
load(matcontent,'Q','X','r','rank','W','H','W0', 'H0');
for combidx = 1:numel(W0) %try every combination
% index the cell arrays:
[~,~,RRE_NMF,T_NMF]=NMF(X,Q,matcontent.Winit{combidx}, matcontent.Hinit{combidx},Tmax,passes);
save( ['output/NMF_',int2str(sNR),'_',int2str(100*Mv),'_',int2str(rank),'_',int2str(i),'.mat'], 'RRE_NMF', 'T_NMF', '-v7.3' );
end
end
I modified your load, because the program couldnt see 'W0'. still i dont know if it works. but when i try to run again i get this message
Dot indexing is not supported for variables of this type.
what am i doing wrongly
Guillaume
Guillaume 2019-1-11
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by