|On this page…|
MATLAB® executes a callback routine defined by the figure's CloseRequestFcn whenever you:
Issue a close command on a figure.
Quit MATLAB while there are visible figures. (If a figure's Visible property is set to off, MATLAB does not execute its close request function when you quit MATLAB; the figure is just deleted.)
Close a figure from the windowing system using a close box or a close menu item.
The close request function lets you prevent or delay the closing of a figure or the termination of a MATLAB session. This is useful to perform such actions as
Displaying a dialog box requiring the user to confirm the action.
Saving data before closing.
Preventing unintentional command-line deletion of a graphical user interface built with MATLAB.
The default callback routine for the CloseRequestFcn is a function called closereq. It contains the statements
if isempty(gcbf) if length(dbstack) == 1 warning(message('MATLAB:closereq:ObsoleteUsage')); end close('force'); else delete(gcbf); end
This callback honors HandleVisibility and therefore does not delete the figure when you use the close command without specifying the figure handle. For example:
h = figure('HandleVisibility','off') close % figure does not close close all % figure does not close close(h) % figure closes
When you quit MATLAB, the current figure's CloseRequestFcn is called, and if the figure is deleted, the next figure in the root's list of children (i.e., the root's Children property) becomes the current figure, and its CloseRequestFcn is in turn executed, and so on. You can use gcbf to specify the figure handle from within a user-written close request function.
If you change a figure's CloseRequestFcn so that it does not delete the figure, issuing the close command on that figure does not cause it to be deleted. Furthermore, if you attempt to quit MATLAB, the quit is aborted because MATLAB does not delete the figure.
If the CloseRequestFcn generates an error when executed, MATLAB aborts the close operation. If an error occurs in a figure's CloseRequestFcn following a quit or exit command, MATLAB aborts the operation to allow any necessary action to be taken.
Use quit force to quit MATLAB unconditionally, regardless of errors in the CloseRequestFcn callback.
The delete command always deletes the specified figure, regardless of the value of its CloseRequestFcn. For example, the statement
deletes all figures whose handles are not hidden (i.e., the figures' HandleVisibility property is not set to off). If you want to delete all figures regardless of whether their handles are hidden, you can set the root ShowHiddenHandles property to on. The root Children property then contains the handles of all figures. For example, the statements
unconditionally delete all figures.
Define the CloseRequestFcn as a function handle. For example,
Where @my_closefcn is a function handle referencing function my_closefcn.
Unless the close request function calls delete or close, MATLAB never closes the figure. (Note that you can always call delete(figure_handle) from the command line if you have created a window with a nondestructive close request function.)
A useful application of the close request function is to display a question dialog box asking the user to confirm the close operation. The following function illustrates how to do this.
function my_closereq(src,evnt) % User-defined close request function % to display a question dialog box selection = questdlg('Close This Figure?',... 'Close Request Function',... 'Yes','No','Yes'); switch selection, case 'Yes', delete(gcf) case 'No' return end end
Now create a figure using the CloseRequestFcn:
To make this function your default close request function, set a default value on the root level.
MATLAB then uses this setting for the CloseRequestFcn of all subsequently created figures.