how to change the Gui close function whithout changing the close function of figures ?

Hi, I'm trying to protect the close request function of my GUI with a yes/no question etc.
I've found some common way to do this in the matlab help :
set(0,'DefaultFigureCloseRequestFcn',@my_closereq).
This works but also changes the close function of the external figures and I don't want to.
I tried: set(hObject,'DefaultFigureCloseRequestFcn',@my_closereq)
But this gives the same problem.
Any Idea ?

 Réponse acceptée

Mathieu - my understanding of the DefaultFigureCloseRequestFcn is that once set at the root level (0), then MATLAB uses this setting for the CloseRequestFcn of all subsequently created figures.
I think what you want to do instead is just modify the CloseRequestFcn for your GUI figure. If you are using GUIDE, then you should click the figure and select (from the menu) View --> View Callbacks --> CloseRequestFcn and the following should appear in the editor
% --- Executes when user attempts to close YourGuiName.
function YourGuiName_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to YourGuiName (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: delete(hObject) closes the figure delete(hObject);
You could then change the body to something like
selection = questdlg('Close YourGuiName?',...
'Close Request Function',...
'Yes','No','Yes');
switch selection,
case 'Yes',
delete(hObject);
case 'No'
return
end
To do something more similar to the code you have written in your question, you could do
set(figure_handle,'CloseRequestFcn','my_closereq')
where figure_handle is the handle to your figure. Note how we use CloseRequestFcn (just like the GUIDE approach) which will affect figure_handle only and not be the default behaviour for all other figures.
Try the above and see what happens!

4 commentaires

Mathieu
Mathieu le 2 Juil 2014
Modifié(e) : Mathieu le 2 Juil 2014
Thanks.
as I said, I used: set(hObject,'DefaultFigureCloseRequestFcn',@my_closereq)
but maybe set(hObject,'CloseRequestFcn',@my_closereq) will work.
Regarding function YourGuiName_CloseRequestFcn
the code generated by matlab was "figure1_CloseRequestFcn" and "figure1_DeleteFcn"
and they cause problem as well.
I'll check again your solutions.
Without knowing what hObject is (you didn't mention that in your question) it could very well be initialized to zero and so using the DefaultFigureCloseRequestFcn would have the same effect as
set(0,'DefaultFigureCloseRequestFcn',@my_closereq)
And yes, I suspected that the code generated by MATLAB would be using something different from YourGuiName and use the default figure1. This was just an example.
I did create a GUI and added the above code to its CloseRequestFcn. There was no problem launching subsequent figures - they closed without any problems (as expected since the new code was "tied" to the GUI only).
If you were still experiencing problems, then it may be because you forgot to remove the line of code that sets the DefaultFigureCloseRequestFcn.
Yep, the problem was in the ' DefaultFigureCloseRequestFcn ' instead of ' CloseRequestFcn '
figure1_CloseRequestfcn seems to be working as well. I think I previously modified figure1_deletefcn which was maybe why it wasn't working properly. It is a little bit confusing because I made those function appear one at a time by clicking on the view callbacks of GUIDE.
I mentioned that " figure1 " was generated, just in case this was a wrong behavior and should have been the filename of the gui instead.
Thanks a lot for the support !

Connectez-vous pour commenter.

Plus de réponses (1)

I agree with Geoff Hayes and that should work. I did a quick example. I first created a function called closeRequest.m which contains the following.
function closeRequest(hObject,event)
ButtonName = questdlg('Close window?', ...
'Close Check', ...
'Yes', 'No','No');
switch ButtonName
case 'Yes'
delete(hObject);
case 'No'
return
end
then tested it with a simple script.
hObject = figure;
set(hObject,'CloseRequestFcn',@closeRequest)
plot(rand(10))
title('check close figure')
hObjects2 = figure;
plot(rand(100))
title('normally close figure')
by doing this only the first 'checked close' figure asks for the close dialog.

Community Treasure Hunt

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

Start Hunting!

Translated by