How do I save multiple dynamically generated variables to the same .mat file?

조회 수: 10 (최근 30일)
A dataset I'm working with makes my computer run out of ram when I try to process the entire thing at once, so I'm trying to process it once cell at a time and save that to a file to prevent a ram shortage. I think my problem is not understanding the proper syntax of the save() function in regards to dynamically generated variable names. The cut down code is below:
for x = 1 : rowLim
for y = 1 :colLim
param(x,y,:) = S1_PParam(C11(x, y), C22(x, y), C12_real(x, y), C12_imag(x, y));
save([path '\output.mat'], 'param(x,y,:)', '-append');
end
end
where rowLim and colLim denote the size of the file I'm working with, and S1_PParam is a custom function that returns a 1 by 1 by 27 array when called with the input parameters shown. I'm currently getting a "param(x,y,:)' is not a valid variable name" error, but I have no idea what the correct variable name would be in this case.
The ideal output would be a single variable in the .mat file that encompasses the entire dataset (e.g. a variable that is a rowLim by colLim by 27 array), but given that seems impossible given how the save() function works I'm just trying to get the output to be a .mat file containing separate 1 by 1 by 27 arrays for each cell in the dataset.
  댓글 수: 2
Ruikai Tang
Ruikai Tang 2021년 5월 25일
I took a quick look at matfile and I'm thought something like the following would work:
m = matfile('output.mat','Writable',true);
for x = 1 : rowLim
for y = 1 :colLim
m.param(x,y,:) = S1_PParam(C11(x, y), C22(x, y), C12_real(x, y), C12_imag(x, y));
end
end
However this gives me a "Cannot use colon ':' to define a dimension of a new variable." error, while removing the colon gives me a "The size of the right hand side did not match the size of the indexing on the left hand side." error instead. I'm kind of lost as to how matfile is supposed to be helpful to me.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2021년 5월 21일
You cannot use save to save part of a variable, except that if you have a scalar struct and you want to save fields as complete variables then you can select the fields to save.
Perhaps matfile would be uuseful to you. If not then you will need to store into variables and save the variables.
  댓글 수: 3
Walter Roberson
Walter Roberson 2021년 5월 25일
m = matfile('output.mat','Writable',true);
for x = 1 : rowLim
for y = 1 :colLim
s1p = S1_PParam(C11(x, y), C22(x, y), C12_real(x, y), C12_imag(x, y));
m.param(x,y,1:length(s1p)) = s1p;
end
end
Ruikai Tang
Ruikai Tang 2021년 5월 25일
Ah, so this creates a temporary variable so that I can have aspecified dimension for the new variable. Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by