3

I am using Matlab Guide to make a user interface. In this interface I run .m files which plots various graphs. After analysis, I want to close the graphs without closing the GUI. If I use close all; all the graphs including the GUI itself closes. However if I use close; GUI closes without closing the figures. How can I resolve this problem?

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
H.K
  • 231
  • 1
  • 2
  • 15
  • 1
    There are various options, depending on your actual implementation. The wisest would be to give every graph containing figure a handle, say `hGraph` and just close that `close(hGraph)`. – Robert Seifert Aug 16 '16 at 11:13
  • Please consider accepting one of the answers to indicate the system that your problem is solved. Thank you! – Robert Seifert Oct 20 '16 at 13:16

2 Answers2

1

This answer from MATLAB Central seems to be most promising:

fh=findall(0,'Type','Figure')

to get the handles of all the open figures. You can use a tag or something to distinguish your gui from the other figures. Then close all others by passing their handle to close.

As suggested in the comments by Hoki, you can probably follow this up with:

close(setxor(fh,the‌​MainGuiHandle))
Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
  • An alternative can be found in another post on Matlab Central, but that seems to be more of a hack: https://nl.mathworks.com/matlabcentral/answers/39546-closing-all-figures-but-not-gui – Dennis Jaheruddin Aug 16 '16 at 11:15
  • 1
    Provided he has the handle of his main GUI somewhere (let's call it `theMainGuiHandle`), then you could add the line: `close(setxor(fh,theMainGuiHandle))` to your answer. – Hoki Aug 16 '16 at 11:21
  • @Dennis Jaheruddin. How can I assign a tag to my GUI and then how can I distinguish it from the rest of the graphs if I use the function referred by you. – H.K Aug 16 '16 at 12:39
  • @Habib.Khan This is hard to answer as I am not in the opportunity to try it now, but here is what I would expect: When you run the command to open the GUI, you should te able to 'catch' the handle. -- Ugly workaround: List all available figure handles, open the GUI, list all now available figure handles and take the setdiff. – Dennis Jaheruddin Aug 16 '16 at 12:45
  • @LuisMendo From the documentation I would expect gcf to only give the current figure (not all available figures). – Dennis Jaheruddin Aug 16 '16 at 12:46
  • 1
    @DennisJaheruddin Oops, I meant `close(setdiff(fh, gcf))` – Luis Mendo Aug 16 '16 at 12:48
  • 1
    @LuisMendo If the current figure is always the GUI (as implied by the question) that should do the trick, but unless it is documented that the GUI is always the current figure I would not be eager to implement it like this. – Dennis Jaheruddin Aug 16 '16 at 12:53
  • @Dennis Jaheruddin. OK I have the handle to all the figures which start from figure (1) to figure (15). The handle to my GUI is (figure1). Now how can I protect my GUI from closing and close all the other figures instead. – H.K Aug 16 '16 at 12:57
1

Assuming you don't have any other axes objects within your GUI, the following will work:

%// find all handles of axes (graphs)
axh = findall(groot,'type','axes')
%// get handles of parent figures containing graphs
fxh = get(axh,'parent')
%// close figures containg axes
close(fxh{:})

It will delete all sub-figures containing an axes object. However I stay with my recommendation: assign distinctive handles to all figure windows and close them explicitely.

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113