Search code examples
matlabplotmatlab-figure

Avoid saving of graphics in Matlab


Starting R2014b Matlab has changed the way how variables are saved using save command; Matlab has also changed the way graphic handles are saved, they are saved as structures now. If you have graphic handles in workspace Matlab takes it longer to save the mat file, size of mat file is large and when you load the file all the saved figures are popped-up, which is irritating to me. It also produces a warning:

Warning: Figure is saved in Oakley_19_PDEparameterEstimation.mat. Saving graphics handle variables can cause the creation
of very large files. To save graphics figures, use savefig. 

I have a simple and straightforward question:

How can I avoid saving of all graphic handles?

Please do not suggest that I can clearvars figure handles before saving them.

Thanks


Solution

  • You can get information about the current workspace variables using whos and save only those variables whose class is not a graphics handle object (i.e. the class name string does not include 'matlab.graphics' or 'matlab.ui'):

    varData = whos;
    saveIndex = cellfun(@isempty, regexp({varData.class}, 'matlab.(graphics|ui)'));
    saveVars = {varData(saveIndex).name};
    save('no_handles.mat', saveVars{:});