Closing all figures associated with a main GUI

3 ビュー (過去 30 日間)
Christine
Christine 2014 年 1 月 5 日
コメント済み: Walter Roberson 2016 年 9 月 30 日
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

回答 (3 件)

Walter Roberson
Walter Roberson 2014 年 1 月 5 日
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 件のコメント
Christine
Christine 2014 年 1 月 6 日
編集済み: Christine 2014 年 1 月 6 日
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 2014 年 1 月 6 日
Yes, hObject and eventdata are names commonly generated by GUIDE, but they are the same thing.

サインインしてコメントする。


Christine
Christine 2014 年 1 月 6 日
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 2016 年 9 月 30 日
To close all figures while you close the GUI main window, you only need to add "close all" in the CloseRequestFcn.
  1 件のコメント
Walter Roberson
Walter Roberson 2016 年 9 月 30 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by