How to save workspace in stages while optimizing disk space
1 view (last 30 days)
Show older comments
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.
0 Comments
Answers (2)
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?
0 Comments
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
See Also
Categories
Find more on Debugging and Analysis 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!