Save into cell or ND array

조회 수: 1 (최근 30일)
fadams18
fadams18 2019년 1월 10일
댓글: Guillaume 2019년 1월 11일
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