How to save workspace in stages while optimizing disk space

3 views (last 30 days)
I have a task that is divided into 2 stages for each I have developed a script, say script A and B.
After satisfactory result from A, I have saved the workspace in a mat file say 'A.mat'.
For next stage, everytime I load A.mat and proceed with the script B which depends on variables from script A. The final result are saved in say 'B.mat'
Now for my analysis, I change some parameter and run script B multiple times and save result to 'B1.mat', 'B2.mat', 'B3.mat', etc.
THE ISSUE: With every save 'B*.mat', variables of 'A.mat' are simply getting repeated. This consumes unnecessary space.
I WISH: To save all variables to 'B*.mat' file with exception of variables stored in 'A.mat'
ALGORITHM:
function saveB_exceptA(fileA,fileB)
% load variable list from 'fileA.mat'
% load current variables list
% compare two list and save only newly created variables to 'fileB.mat'
end
I could do this with fetching strings corresponding to variable names... basically a tedius file handling code, but really wish for a simpler matlab syntax as alternative.

Answers (2)

the cyclist
the cyclist on 29 Sep 2022
The save command has a syntax option that allows you to specify a list of variables to save, rather than saving all variables in the workspace. Does that help?

Steven Lord
Steven Lord on 29 Sep 2022
How are you loading your data? Are you calling load with an output argument or without?
If you called load with an output argument all the variables from A.mat will be fields of one struct array in the workspace. You could clear that variable before saving or use the -regexp option to exclude that struct array.
Another alternative would be to pack the variables you've created in the function into a struct array and use the -struct option to save the fields of that struct as independent variables in the B.mat file.
  1 Comment
Navinder Singh
Navinder Singh on 1 Oct 2022
Edited: Navinder Singh on 1 Oct 2022
Thanks a lot.
Right now I am using below code:
Example Code 1
% load file A
zzzzz.loadfile = 'A.mat';
load(zzzzz.loadfile)
%--------------------------------
% new calculations - script B
%-------------------------------
% save file without overwrite, without variables repeat
zzzzz.savefile = 'B.mat';
zzzzz.diff = setdiff(who,who(matfile(zzzzz.loadfile))); % find list of new variables
save(zzzzz.savefile,zzzzz.diff{1:end-1}) % end-1 to skip last entry of 'zzzzz'
As you suggested, I could modify the code as below.
Example code 2:
% load file A
zzzzz.loadfile = ;
dataA = load('A.mat')
%--------------------------------
% new calculations - script B
% access variables from A.mat as 'dataA.var1'
%-------------------------------
% save file without overwrite, without variables repeat
save('B.mat','-regexp', '^(?!(dataA)$).')
Altthough I would still prefer the first example code as it retains the way variables are accessed throughout the two scripts A and B.
If possible pls suggest a better version of the first example code shown by me.

Sign in to comment.

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!