The documentation for the save
command says that you should delete figures if you don't want to bog down the *.mat
file. I save
to a *.mat
file periodically, and I re-use my figure after issuing clf
. I would prefer not to have to delete it just to save
a *.mat
file, then open a new figure. Is there a way to do this?
Save Matlab workspace without saving or deleting figures
Asked Answered
You can either save the variables you want explicitly when calling save
if you know all the variables you'd like to save.
save('output.mat', 'variable1', 'variable2', 'variable3');
Alternately, if you want to save all variables in your workspace that aren't graphics handles, something like this could work:
% Get a list of all variables
allvars = whos;
% Identify the variables that ARE NOT graphics handles. This uses a regular
% expression on the class of each variable to check if it's a graphics object
tosave = cellfun(@isempty, regexp({allvars.class}, '^matlab\.(ui|graphics)\.'));
% Pass these variable names to save
save('output.mat', allvars(tosave).name)
This will not save any figures (or any graphics objects) and also will allow you to keep them open.
Thank you, Suever! With all that kind of coding, there really should be a standard option to exclude graphics in saving. Fortunately, from using your code, my
*.mat
file is only marginally smaller, so I can forgo the extra code to exclude graphics. But it's good to have ruled that out. –
Reorientation @Reorientation In general, it's a bad idea to save everything while you have variables that are graphics handles because when you load that file again, all graphics will be re-created. I.e. even if you have a figure open, load will open another figure –
Hoboken
If you want to diagnose what is the large thing in the *.mat file, use the output of
whos
to check the size of each variable –
Hoboken Wow. Hooda thunk that ye olde
whos
command could be so useful. The culprit is as I feared -- my historical record of the state of key objects. No getting around that. As for saving everything, I don't have variables that store graphics handles. But I see your point. I shall beware of that going forward. Thanks. –
Reorientation This doesn't seem to work with MATLAB 2015a . I have open figure windows with callbacks, but there's no visible item in my workspace which triggers
saveIndex
to be zero instead of one. –
Wittol Ahhh,, my bad; I found a struct with figure handles two levels down. I'll have to write some recursive thingy to find and avoid those. Or make sure to create my own custom class structure and add that to the list of variables to ignore when saving my workspace. –
Wittol
I tried the commands given, except without the semicolons, and found none of my variables negated on the
tosave
list. I looked at content of the allvars
variable, and realized that all the figure handles were fields in structs, like @CarlWitthoft. I'm in a hurry, so I'll just omit all the structs - tosave = cellfun(@isempty, regexp({allvars.class}, 'struct'))
. If I need to save some structs, they are much fewer, and I can save them specifically in another MAT. –
Gyromagnetic © 2022 - 2024 — McMap. All rights reserved.
*.mat
file size. – Reorientation