8

I am trying to determine if there is a nice way, to close all figures in MATLAB, except for one(s) that I determine before hand, are not to be closed. Is there such a way to do this?

I am finding that I am wasting a lot of time chasing down specific stuff to close, every time my MATLAB script runs. Thank you.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Spacey
  • 2,941
  • 10
  • 47
  • 63
  • 4
    Google search resulted in the following link, which has the function that does exactly what you are looking for: http://www.mathworks.com/matlabcentral/fileexchange/24420-close-all-figures-except-those-listed – Alexey May 21 '13 at 22:54
  • glad to help, I posted it as answer. – Alexey May 21 '13 at 23:03

2 Answers2

15

You can try this

%figures to keep
figs2keep = [4, 7];

% Uncomment the following to 
% include ALL windows, including those with hidden handles (e.g. GUIs)
% all_figs = findall(0, 'type', 'figure');

all_figs = findobj(0, 'type', 'figure');
delete(setdiff(all_figs, figs2keep));

Here's the link to the source

Alexey
  • 5,898
  • 9
  • 44
  • 81
3

Probably the safest way is to assign handles to variables h1, h2, ... for each of your figures as you generate them and then use close(handle) to close the figures you don't want open.

close() also takes a vector/matrix of handles as an input, so you could always aggregate a vector of handles of figures to be closed.

Ryan J. Smith
  • 1,140
  • 8
  • 15
  • Well, the problem is that things are changing very quickly, this isnt a 'nice' environment, lots of research going on. So more often than not, I have umpteen figures open, but I want all closed now except for figures 7 and 4. So, I am hoping for a solution that would just take those two arguments, [4 7], and close everything else. – Spacey May 21 '13 at 22:45