Closing all figures associated with a main GUI

4 views (last 30 days)
I am programming a GUI that consists of a main window and several other figures. I want that all figures associated with this GUI will close when I close the main window.
So far I tried to use the CloseRequestFcn to do so but I have problems to assign the handles structure that contains the handles to the other figures to the CloseRequestFcn:
if true
set(h.mainControlsFigure,'CloseRequestFcn',@Interferometer_8_main_close_Fcn, h)
end
creates an error in matlab.
On the other hand the matlab documentation gives an example for the use of the CloseRequestFcn where two arguments (src and evnt) are assigned whichs meaning is not clear to me:
if true
Interferometer_8_main_close_Fcn(src,evnt)
end
Furthermore, in which case should I use the CloseRequestFcn and in which case the deleteFcn?
I would be very grateful if somebody could bring some light into the darkness of the CloseRequestFcn and maybe suggest a suitable solution.
Thanks, Christine

Answers (3)

Walter Roberson
Walter Roberson on 5 Jan 2014
set(h.mainControlsFigure,'CloseRequestFcn',@(src,evnt) Interferometer_8_main_close_Fcn(src, evnt, guidata(h.mainControlsFigure))
For almost all callbacks, MATLAB automatically supplies two arguments, the first of which is the object that the callback relates to, and the second of which is a structure that has some details about the callback (for example which key was pressed for a key-press callback).
The form of the code I used above is needed so that the extra handles structure that gets passed to your callback is an up-to-date version at execution time of the callback instead of it being set at the time the callback is created.
  2 Comments
Christine
Christine on 6 Jan 2014
Edited: Christine on 6 Jan 2014
Thanks for your answer!
Aha, so it is the same as hObject and eventdata in usual callbacks?
I ususally update the handles structure in the first line of the callback, but I also like your solution.
Walter Roberson
Walter Roberson on 6 Jan 2014
Yes, hObject and eventdata are names commonly generated by GUIDE, but they are the same thing.

Sign in to comment.


Christine
Christine on 6 Jan 2014
Oh how embarrassing, I just forgot the curly brackets... So to assign the handles structure to the CloseRequestFcn it has to be:
set(h.mainControlsFigure,'CloseRequestFcn',{@Interferometer_8_main_close_Fcn, h})
Now in order to close all the associated figures this works for me:
function Interferometer_8_main_close_Fcn(src,evnt, h)
h = guidata(h.mainControlsFigure); % loads the full handles structure
choice = choose_dialog('Are you sure you want to close the program Interferometer_8.m?', ...
'Yes' , 'No');
if choice == 1 % answer 'yes'
% go through all handle fields and check if they are figure handles. If so, close the figure
dummy = fieldnames(h);
numberOfFields = length(dummy);
for i = 1 : numberOfFields
name = dummy{i};
if strcmp(name, 'mainControlsFigure')
% do not delete main controlsFigure as handles structure h is saved to
% mainControlsFigure
elseif isfigurehandle(h.(name))
delete(h.(name))
end
end
% finally delete also mainControlsFigure and clear the handles structure h
delete(h.mainControlsFigure)
clear('h');
elseif choice == 2 % answer 'no'
return
end
end
I use to update the handles structure in the first line of my functions but also the solution of Walter looks elegant.
I also had to be careful with the deleting of figures as the handles structure is assigned to one of them (mainControlsFigure). So this figure should be deleted as last as otherwise all the handles are lost.

Jonathan
Jonathan on 30 Sep 2016
To close all figures while you close the GUI main window, you only need to add "close all" in the CloseRequestFcn.
  1 Comment
Walter Roberson
Walter Roberson on 30 Sep 2016
Caution:
  1. "close all" does not close hidden figures. You need "close all hidden" to close hidden figures.
  2. "close all" is going to close all non-hidden figures, which would also affect figures not associated with the main GUI. If you are creating a system with multiple figures then you may need to be more selective about what you close.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks 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!