The Wayback Machine - https://web.archive.org/web/20110828091217/http://www.mathworks.com:80/help/techdoc/creating_plots/f7-21193.html
Skip to Main Content Skip to Search
Product Documentation

The Figure Close Request Function

Introduction

MATLAB 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 a function 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.

Redefining the CloseRequestFcn

Define the CloseRequestFcn as a function handle. For example,

set(gcf,'CloseRequestFcn',@my_closefcn)

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:

figure('CloseRequestFcn',@my_closereq)

To make this function your default close request function, set a default value on the root level.

set(0,'DefaultFigureCloseRequestFcn',@my_closereq)

MATLAB then uses this setting for the CloseRequestFcn of all subsequently created figures.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A; sessions led by MATLAB experts.

 © 1984-2011- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS