MATLAB Answers


How to hide a figure instead of deleting it?

Asked by Chaitanya Jha on 18 Mar 2019
Latest activity Commented on by Adam
on 18 Mar 2019
Is there any way I can change the DeleteFcn of a figure to not delete the figure but only make it invisible?


Chaitanya - can you describe the use case for this? Are you closing a GUI (figure) that you wish to hide rather than delete? Or is there a figure (with axes or whatever) that you are "closing"? Please clarify.
Hi Geoff,
I am making a GUI in which I have many different panels with UI Controls and I also have a few axes. Initially when the GUI is launched all these panels and axes are in the one BIG figure, but I need to add a toggle button to seperate all the UI Panels into their respective SMALL figures so that they can be freely moved around if needed. I do this by changing the parent property of the UI Panels. When the SMALL figures are active then I hide the BIG orginal figure by setting it's Visible property to 'off'. When the toggle button is pressed again (this toggle button is still available in one of the small figures), all the panels recombine in the BIG figure and all SMALL figures become invisible.
I cannot add and delete SMALL figures dynamically because I am using functions such as WindowScrollWheelFcn and WindowButtonMotionFcn. Due to this reason I create all the figures while starting the GUI and then later set their respective visibile properties according to my needs.
The problem is what if while one of the SMALL figures is active the user clicks the window close button on that figure and it deletes the entire figure along with it's contents. I can add a DeleteFcn for the SMALL figures to transfer it's UI Panel to the parent BIG figure before closing, however this makes me loose my WindowScrollWheelFcn and WindowButtonMotionFcn functionality attached to the respective SMALL figure. To preserve these functionality I was hoping to redirect the delete function to just hide the SMALL figure so that it does not cause any errors in my GUI.

Sign in to comment.

1 Answer

Answer by Adam
on 18 Mar 2019
Edited by Adam
on 18 Mar 2019
 Accepted Answer

If you over-ride the CloseRequestFcn you can have it do whatever you tell it. In general you should always have this function call delete on the figure if you over-ride it because that is expected behaviour and if you don't the figure will not get deleted.
So if you create a CloseRequestFcn that just does something else and does not delete the figure than it will stay alive and you can switch its 'Visible' property off and do whatever else you wish.
I do this for docking and undocking panels in one of my workflows. I have a panel that is undockable and, because it is part of the workflow I don't want the user to simply be able to delete the panel when it is an undocked figure I program the CloseRequestFcn to redock it in the main figure.
The interaction between DeleteFcn, CloseRequestFcn and one or two others that are similar can be confusing, but they are explained in the help as to when each is called and what its function is. I never remember so have to look it up each time I want to do something out of the ordinary with this.


Show 1 older comment
on 18 Mar 2019
Be careful to ensure something has responsibility for deleting the figure ultimately though otherwise you can end up with a lot of invisible figures that you don't realise are there until you do something like
hFigs = findall( groot, 'type', 'figure' )
and see a big list even though visually you can't see a single figure! And invisible figures will still have all their graphics objects and any attached data taking up memory.
Yeah, I understand that. I have a delete function to delete all the figures that I have created. I execute this function when the user tries to close the BIG figure.
on 18 Mar 2019
Sounds good. That is exactly what I do too ;-)

Sign in to comment.