Search code examples
matlabplotmatlab-figure

Possible to save figures into variables instead of files in Matlab?


I have Matlab code such as:

f=figure(1)
plot(x,y)
hold on
plot(x1,y1)
hold on
...
plot(xf, yf)
%want to save this into a VARIABLE, NOT into a .fig, .png, jpg, or other kind of file

So if it is possible to save this into some variable var_fig, I would like to later open the figure (such as in a subplot with multiple saved figures/plots) doing something like

showfig(var_fig)

Is this possible in Matlab?


Solution

  • It's possible but not recommended. You can save any variable using save:

    myfigure = gcf; %store current figure in a variable
    save('filename','myfigure');
    

    However according to Matlab be warned:

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

    Here's how to use savefig