Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

The Figure Close Request Function

Introduction

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

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

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 closes

Quitting the MATLAB Environment

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.

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

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.

  


Recommended Products

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