Clear Filters
Clear Filters

closeRequestFcn: how do I distinguish if it is called by a mouse click to window's close button vs a close() command??

5 views (last 30 days)
I am writing a custom closeRequestFcn
I would like it to do different things depending if it is called because the user
1) clicked the close button on the figure -- clearly they want to close this figure, so let them
2) called e.g. close all to close all figures -- in this case do not close this figure
Note I can't use the handle visibility property, as the figure needs to be accessible for other reasons.
What I've tried: the event argument passed to the callback is the same in these cases the figures CurrentPoint property is the same in both cases
Any ideas? Java magic? some other property to test?

Accepted Answer

Jan on 10 Aug 2014
Edited: Jan on 10 Aug 2014
The most direct solution would be to avoid the brute close all, when you do not want to close all figures. The idea of calling "close all except for the one I do not mean" is magic.
Checking the value of gcbf does not help, because it is set by close all also.
But you can check if one of the calling function is close by dbstack:
Stack = dbstack;
Caller = {};
calledByClose = any(strcmp(Caller, 'close'))
But I repeat that this is magic, because the CloseRequest function tries to guess, what the user really wants instead of the commands he types. This is beyond an intuition and other users of your code will tend to desperate when they try to close the figure.
What about setting a flag e.g. in the figure's Application data and create a personal function for closing all figures, which do not contain this flag?
function smartCloseAll % Choose a more convenient name...
FigList = allchild(0);
for iFig = 1:length(FigList)
aFig = FigList(iFig);
AppData = getappdata(aFig);
kill = true;
if isfield(AppData, 'myCloseRejectFlag')
kill = not(AppData.myCloseRejectFlag)
if kill
  1 Comment
John Iversen
John Iversen on 11 Apr 2015
Hi, Thanks for the answer (which I just saw, belatedly!). Both are good solutions, and I'll implement #1.
You're right to point out that this is magical behavior. I should have been clearer in the description, but the window I don't want closed is a GUI, while all other windows contain figures. The GUI deserves to be more permanent and I felt the close all behavior wouldn't be too unexpected. Plus the magic would be welcome: currently all users are doing 'close all; redraw gui' which is a bit silly!
I'll choose magic over silly :)

Sign in to comment.

More Answers (0)


Find more on Historical Contests in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!