H.K
H.K

Reputation: 241

How to close all graphs in GUI without closing the GUI itself?

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?

Upvotes: 3

Views: 770

Answers (2)

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21563

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))

Upvotes: 1

Robert Seifert
Robert Seifert

Reputation: 25232

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.

Upvotes: 1

Related Questions