Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save Matlab workspace without saving or deleting figures

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?

like image 695
user36800 Avatar asked Mar 12 '23 20:03

user36800


1 Answers

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.

like image 154
Suever Avatar answered Mar 19 '23 13:03

Suever