Recreating a figure from its handle after it has been closed

46 views (last 30 days)
Hi All,
I have a function that creates a figure as below
mainfig = myfunction(x,y)
mainfig = figure('NumberTitle', 'off', 'Name', 'MyFigName');
h.tabgroup = uitabgroup(mainfig);
h.tab = uitab(h.tabgroup, 'Title', 'MyTabName');
h.panel = uipanel('Parent',h.tab);
ax = subplot(2,5,[1 6],'Parent',h.panel);
plot(x,y,'Parent',ax);
end
Now suppose the user calls the function, looks at the figure and then closes it (clicking on the " x " of the figure, top right corner)
I am then left (in the workspace) with the figure handle (mainfig)
Is it possible to re-open/re-create the figure as it was when generated? If yes, could you show an example?
If not, is there any other data I need to add in the output of myfunction in order to achieve that?
Thanks in advance
G

Answers (3)

Kelly Kearney
Kelly Kearney on 31 Aug 2018
No, the figure handle itself does not contain that information. The graphics objects handles loose their link to the relevant data when the objects are deleted. You would have to save the figure itself (before it was deleted), via savefig... this saves the relevant data needed to recreate the file. Alternatively, you could save the input data used to create the figure (in your example, the x and y variables) and recall the plotting function.

Dennis
Dennis on 31 Aug 2018
You can change the CloseRequestFcn of your figure to turn your figure invisible rather than closing it. As long as you keep your handle around you can turn it visible again anytime you feel like it.
f=figure;
a=axes;
imshow('autumn.tif','Parent',a)
f.CloseRequestFcn=@savemyfigure;
function savemyfigure(hObj,~)
hObj.Visible='off'
end
Just one thing: this kinda prevents the figure from getting closed...you might want to consider another function to actually close it.

Diêgo Carneiro
Diêgo Carneiro on 23 Feb 2022
Edited: Diêgo Carneiro on 23 Feb 2022
You can save the figure parameters in a struct using get(mainfig). For example:
fighandle = get(mainfig);
However, you can't recreate the figure just using fighandle, but you will have access to the parameters. If you want to create a new figure based on the previous one, you could do as follows:
fighandle = get(mainfig);
fig2 = figure;
set(fig2,'Name',fighandle.Name,'Units',fighandle.Units)
Ps: I am working in a function to transform the struct in a figure object

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products


Release

R2015b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!