| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
| On this page… |
|---|
Quitting the MATLAB Environment |
MATLAB software 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 an M-file called closereq. It contains the statements
if isempty(gcbf)
if length(dbstack) == 1
warning('MATLAB:closereq',...
'Calling closereq from the command line is now obsolete,...
use close instead');
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 closesWhen 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. 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.
The delete command always deletes the specified figure, regardless of the value of its CloseRequestFcn. For example, the statement
delete(get(0,'Children'))
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
set(0,'ShowHiddenHandles','yes') delete(get(0,'Children'))
unconditionally delete all figures.
![]() | Controlling Graphics Output | Saving Handles in M-Files | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |