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

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 Comments

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.

Sign in to comment.

 Accepted Answer

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 Comments

I'm trying to save the entire param variable, the reason for the indexing is because I thought that is how I was supposed to dynamically generate variable names according to https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval.
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.
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
Ah, so this creates a temporary variable so that I can have aspecified dimension for the new variable. Thanks!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!