Closing all figures, but not GUI

47 ビュー (過去 30 日間)
Khawaja Asim
Khawaja Asim 2012 年 5 月 27 日
回答済み: Gordon Fox 2023 年 2 月 21 日
How do I use "close command" to close all the other figures, but not the main GUI?
Actually here is a case in which a main GUI is opened and some other figures too. If I use "close all" it closes all figures including the GUI. Show me a way to close only the other figures.

採用された回答

Image Analyst
Image Analyst 2012 年 5 月 27 日
I think you should be able to set handle visibility of your gui to 'off' then close all (which now won't see your figure) then turn it back on. You can do this:
set(handleToYourMainGUI, 'HandleVisibility', 'off');
close all;
set(handleToYourMainGUI, 'HandleVisibility', 'on');
Here's a fully tested demo, guaranteed to work:
% Open 8 figures.
figure(1);
% Create figure 2 and get it's handle.
% We're not interested in getting the handles of the other figures.
h2 = figure(2);
figure(3);
figure(4);
figure(5);
figure(6);
figure(7);
figure(8);
% Now close all except figure 2.
% First make figure 2 invisible.
set(h2, 'HandleVisibility', 'off');
% Close all visible figures. This won't include figure 2
uiwait(msgbox('Click OK to close all but figure 2'));
% Close all visible figures. This won't include figure 2
close all;
% Now make figure 2's handle visible again.
set(h2, 'HandleVisibility', 'on');
  4 件のコメント
Image Analyst
Image Analyst 2016 年 12 月 14 日
If you want to make your axes vanish with GUIDE, I think the best way is to make the axes contained within a panel and then set the visibility of the panel to 'off'. Simply setting the visibility of the axes or doing "axis off" doesn't seem to work because the axes is a whole bunch of things, namely the x and y axes themselves, the tick marks and tick labels, the contents of the axes (line plots, images, text strings, etc.), the title, the x and y labels, etc. You'd have to set all of those things' visibility. It's much easier to just put the whole thing into a panel and set the panel visibility with one call.
R G
R G 2016 年 12 月 15 日
編集済み: R G 2016 年 12 月 15 日
i am really not sure about that. But i think close clause also kill Guide panel. So even if i set visibility off for any panel on Guide, it will be closed also. I think I need to set visibility off for Guide or I need another "close clause" what kill only figure but not Guide, right.

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

その他の回答 (7 件)

Omri Mamo
Omri Mamo 2016 年 12 月 14 日
編集済み: Omri Mamo 2016 年 12 月 14 日
I think the best way to do it is by the 'Tag' property:
% get( handles.output , 'Tag' ) is the 'Tag' of the GUI
Figures = findobj( 'Type', 'Figure' , '-not' , 'Tag' , get( handles.output , 'Tag' ) );
NFigures = length( Figures );
for nFigures = 1 : NFigures;
close( Figures( nFigures ) );
end;
  1 件のコメント
Allen
Allen 2017 年 8 月 17 日
Omri,
I was using a similar approach in looking for figures and closing only those without tags, albeit your approach is much more elegant. I like the way you used the inclusive/exclusive features in findobj(...). Your method can also be simplified to the following:
if true
% get( handles.output , 'Tag' ) is the 'Tag' of the GUI
Figures = findobj('Type','Figure','-not','Tag',get(handles.output,'Tag'));
close(Figures)
end

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


per isakson
per isakson 2012 年 5 月 27 日
There is no simple way to do that in Matlab.
In the FEX there are some contribution, which try to provide help, e.g. fig.m / closefig.m and Close all figures except those listed. There are more.
You can make your own function. Here is something to getting started. Put it under a shortcut on the toolbar. Refine it successively to meet new needs. Replacing findobj with findall might not be a good idea.
function keep_guis
fig_h = permute( findobj( 0, 'Type', 'Figure' ), [2,1] );
for fh = fig_h
uih = findobj( fh, 'Type', 'uicontrol' );
if isempty( uih )
delete( fh );
end
end
end
Here a GUI is defined as a figure with a visible uicontrol.

Khawaja Asim
Khawaja Asim 2012 年 5 月 27 日
I have done this now using "close 1"
this closes the Figure 1. Every time displaying a new figure, I write this.. this closes the Figure 1 and opens every new figure as Figure 1... :P
what you say about this trick?? :P
  4 件のコメント
Khawaja Asim
Khawaja Asim 2012 年 5 月 27 日
@Image Analyst: I have noticed that you are very quick in logic making and algo developing... I am working on retina images for Optic Disc detection and Fovea detection. I almost have completed my work.. Now working on GUI to make it presentable for demonstration......... Have you also worked in this field? To which university or institute you belong?
Image Analyst
Image Analyst 2012 年 5 月 27 日
I have a Ph.D. in optics. During my Ph.D. I worked on a laser scanning ophthalmoscope for a year or two, though my main research was in radiology. I do algorithm development (and GUI development) full time as my job and have been doing to for over 30 years. I develop apps for other people in our company to run to do their own image analysis. I don't want the tedious job of analyzing their images - I'll leave that up to them and I'll just develop the program for them. You can look up my profile to get as much information as I want to share publicly. I work in the USA. Good luck in your research.

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


Mike Swickrath
Mike Swickrath 2016 年 12 月 1 日
Hi all,
I had a different situation. I have figure numbers that dynamically change. Depending on the user, and their session in the GUI I developed, the figure numbers take on integer values in an arbitrary way (...an artifact of not knowing I would want to close all open figures except the GUI before I started programming the GUI).
Many of the solutions above probably would have worked in my case if I would have identified a need for this functionality about 5000 lines of code before now. Alas, I am in a different situation and needed to be a little more creative. I hope this helps some of you.
Anyway, my solution was to use a combo of Daniel's technique along with a technique to count figures posted elsewhere ( counting figures ).
Mike
% Detect all figures - close the figures that are not the GUI
fh=findall(0,'type','figure');
fh=findobj(0,'type','figure');
nfh=length(fh); % Total number of open figures, including GUI and figures with visibility 'off'
% Scan through open figures - GUI figure number is [] (i.e. size is zero)
for i = 1:nfh
% Close all figures with a Number size is greater than zero
if sum(size(fh(i).Number)) > 0
figure(fh(i).Number);
close(gcf);
end
end
  2 件のコメント
Larry McDermott
Larry McDermott 2017 年 1 月 10 日
Mike: Your code was exactly what I was looking for, worked like a charm! Thanks!
Larry
Bhavanithya Thiraviaraja
Bhavanithya Thiraviaraja 2022 年 11 月 16 日
Thank you this worked for me.
Additional tip: If you don't want the figure to pop up in the line figure(fh(i).Number), you can just give close(fh(i).Number) directly

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


Daniel
Daniel 2014 年 5 月 14 日
I was using the publish command to write hundreds of figures into an HTML file. I found the accepted answer here to be problematic then. But I used a neat work-around:
In case you are able to count the number of figures, which should be rather straight forward by using a global parameter that can be increased in any function where you are plotting figures, there is a very simple trick to close all figures except the GUI.
for i = 1:no_of_open_figures
close(gcf)
end
This trick is based on the assumption that all your figures will be on top of the GUI, which should always be the case except if there is interaction with the GUI between plotting.

Andreas Martin
Andreas Martin 2018 年 5 月 9 日
I use the following code to close all figures but first:
h = findall( 0, 'type', 'figure' ); % Find all figures
h = setdiff( h, 1 ); % Exclude first figure (1)
h.delete % Delete 'em

Gordon Fox
Gordon Fox 2023 年 2 月 21 日
I had the same issue with a GUI and fixed it by including the following setting in the main GUI figure creation.
'HandleVisibility', 'callback'

カテゴリ

Help Center および File ExchangeSubplots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by