user36800
user36800

Reputation: 2259

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?

Upvotes: 9

Views: 3886

Answers (1)

Suever
Suever

Reputation: 65430

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.

Upvotes: 14

Related Questions