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

2 次查看(过去 30 天)
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?
Thanks!

采纳的回答

Jan
Jan 2014-8-10
编辑:Jan 2014-8-10
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 = {Stack.name};
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)
end
if kill
close(aFig);
end
end
  1 个评论
John Iversen
John Iversen 2015-4-11
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 :)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Historical Contests 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by