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

8 visualizzazioni (ultimi 30 giorni)
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 Commenti
Ruikai Tang
Ruikai Tang il 25 Mag 2021
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.

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 21 Mag 2021
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 Commenti
Walter Roberson
Walter Roberson il 25 Mag 2021
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 il 25 Mag 2021
Ah, so this creates a temporary variable so that I can have aspecified dimension for the new variable. Thanks!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Workspace Variables and MAT Files in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by