How do I create a button in a pop up that will allow a user to close all figures?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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
Paulo Silva
on 7 Feb 2011
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
Andrew Newell
on 7 Feb 2011
Of course, that would close any figures not opened by the tool as well.
Paulo Silva
on 7 Feb 2011
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
Paulo Silva
on 7 Feb 2011
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'))
Saleem Infinity
on 8 Feb 2011
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?
Paulo Silva
on 8 Feb 2011
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.
Saleem Infinity
on 8 Feb 2011
Thank you Paulo for ur reply
i have tried
close(findobj('tag','myguifigure'))
but it give me following error:
Invalid figure handle.
Any idea? :(
Paulo Silva
on 8 Feb 2011
I'm just guessing but it seems that you didn't tagged the figures properly, you tagged axes instead of figures.
Saleem Infinity
on 8 Feb 2011
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: ''
Paulo Silva
on 8 Feb 2011
get(h1,'Type') %h1 isn't the figure handle
Paulo Silva
on 8 Feb 2011
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');
Saleem Infinity
on 8 Feb 2011
Aha!Ok everything is clear now
Thank you so much for all your help I really appreciate it.
Brett Shoelson
on 8 Feb 2011
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
More Answers (0)
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Products
Tags
See Also
on 7 Feb 2011
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)