How do I create a button in a pop up that will allow a user to close all figures?

I have a simulation tools which calls MATLAB functions. These functions produce many plots. The simulation will not stop until I close all the figures.
Is there a way to add a command in a pop up with a button asking the user if he want to close all the figures? I would like yes/no buttons with all figures closing when the user answers yes.

 Accepted Answer

choice = questdlg('Would you like to close all figures', ...
'Close figures', ...
'Yes','No','No');
switch choice
case 'Yes'
disp('All figures closed')
close all
case 'No'
disp('No figures closed')
end

12 Comments

Of course, that would close any figures not opened by the tool as well.
In that case the OP needs to tag the figures or just give them specific names so he/she can know what figures should be closed
Here's how to tag figures
set(figurehandle,'Tag','myguifigure')
and how to close all figures with that specific tag
close(findobj('type','figure','tag','myguifigure'))
Thank you soooo much Paulo :) i have tried the first code and it works fine :) :)
with 'tag' it didn't work with me,I guess I did it the wrong way!
for testing,i create a figure, then i set the property 'tag' as u've mentioned,then if i use findobj,
findobj('type','figure','tag','myguifigure')
Empty matrix: 0-by-1
which is mean object not found?!
findobj('type','figure')
ans = 1
findobj('tag','myguifigure')
ans =171.0059
do i have to edit any other property to make it works?
It's working fine here but you can use:
close(findobj('tag','myguifigure'))
It will search all objects but the only ones with the tag will be the ones you want to close so it will work the same way.
Thank you Paulo for ur reply
i have tried
close(findobj('tag','myguifigure'))
but it give me following error:
Invalid figure handle.
Any idea? :(
I'm just guessing but it seems that you didn't tagged the figures properly, you tagged axes instead of figures.
this is my code actually:
x= -2*pi :1/5:2*pi;
y=sin(x);
h1=plot(x,y);
set(h1,'tag','myguifigure');
close(findobj('type','figuers','tag','myguifigure'))
get (h1)
-----------------
after executing,the figure still open!
and this is the object property:
DisplayName: ''
Annotation: [1x1 hg.Annotation]
Color: [0 0 1]
LineStyle: '-'
LineWidth: 0.5000
Marker: 'none'
MarkerSize: 6
MarkerEdgeColor: 'auto'
MarkerFaceColor: 'none'
XData: [1x63 double]
YData: [1x63 double]
ZData: [1x0 double]
BeingDeleted: 'off'
ButtonDownFcn: []
Children: [0x1 double]
Clipping: 'on'
CreateFcn: []
DeleteFcn: []
BusyAction: 'queue'
HandleVisibility: 'on'
HitTest: 'on'
Interruptible: 'on'
Selected: 'off'
SelectionHighlight: 'on'
Tag: 'myguifigure'
Type: 'line'
UIContextMenu: []
UserData: []
Visible: 'on'
Parent: 170.0105
XDataMode: 'manual'
XDataSource: ''
YDataSource: ''
ZDataSource: ''
get(h1,'Type') %h1 isn't the figure handle
Here's an hint about the problem
get(h1,'Type') %ans is line
b=get(h1,'Parent');
get(b,'Type') %ans is axes (line inside axes)
c=get(b,'Parent');
get(c,'Type')
%now you get the handle for the figure
when you plot using the plot function matlab creates a figure with an axes inside, you can create that figure and axes yourself
MyFigHandle=figure;
MyAxesHandle=axes;
set(MyFigHandle,'CurrentAxes',MyAxesHandle);
set(MyFigHandle,'tag','myguifigure');
Aha!Ok everything is clear now
Thank you so much for all your help I really appreciate it.
If you're looking for the handle of the figure that is the parent of object h, consider the |ancestor| function:
f = ancestor(h,'figure','toplevel');
Cheers,
Brett

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!