Graphics    

The Close Request Function

MATLAB executes a callback routine defined by the figure's CloseRequestFcn whenever you:

The close request function enables you to prevent or delay the closing of a figure or the termination of a MATLAB session. This is useful to perform such actions as:

The default callback routine for the CloseRequestFcn is an M-file called closereq. It contains the statements

This callback disables HandleVisibility control by setting the root ShowHiddenHandles property to on, which makes all figure handles visible.

Quitting MATLAB

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, therefore, use gcf to specify the figure handle from within the close request function.

If you change a figure's CloseRequestFcn so that it does not delete the figure (e.g., defining this property as an empty string), then 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.

Errors in the Close Request Function

If the CloseRequestFcn generates an error when executed, MATLAB aborts the close operation. However, errors in the CloseRequestFcn do not abort attempts to quit MATLAB. If an error occurs in a figure's CloseRequestFcn, MATLAB closes the figure unconditionally following a quit or exit command.

Overriding the Close Request Function

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 HandleVisibility property is 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.


  Protecting Figures and Axes Handle Validity Versus Handle Visibility