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일

1 개 추천

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

If i need to test all values of W0 and H0. How do i index them? In effect I wil have a 5 matfiles
every mat file has the following content
data1
W
H
H0_2 ... H0_20
W0_2 ... W0_20
so I want to experiment every possible combination of (W,H) & (W0,H0).
So for each data file i load, i will pass W, H and H0_2 W0_2 ( save this results) and then update the next values W, H, H0_4, W0_4 ... till H0_20, W0_20
Then i load the next data file data2. and do the same thing. still data5
% i will feed the data into this funtion like this for all combinations
[error, time ]=NMF(W,H,W0_2, H0_2);
What you describe is exactly what I said would be hard if you'd use numbered variables (W0_2, W0_4, etc.) and is trivial to do using indexing:
for fileidx = 1:5
matcontent = load(fullfile('DATA', sprintf('data_%d.mat',fileidx))); %load mat file with W, H, W0, H0 and V
for combidx = 1:numel(W0) %try every combination
%simply index the cell arrays:
someresult = dosomething(matcontent.W, matcontent.H, matcontent.H0{combidx}, matcontent.W0{idx});
%...
end
end
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
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개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2019년 1월 10일

댓글:

2019년 1월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by