View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Close all figures except those listed

5.0 | 14 ratings Rate this file 13 Downloads (last 30 days) File Size: 1.46 KB File ID: #24420 Version: 1.1

Close all figures except those listed


Karl (view profile)


11 Jun 2009 (Updated )

Close all figures except those listed by the user.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

Currently, if you have many figures open at once, but decide you only want to keep one or two of them, you have to manually close all the other figures. This function is a simple tool for keeping the figures you want and closing all the others. Also, called with no arguments, 'cab' is a convenient, three-letter replacement for 'close all'.


This file inspired Causal State Modeller Toolbox and Close All Figures Except Specific (Previously Tagged) Ones.

MATLAB release MATLAB 7.5 (R2007b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (16)
15 Dec 2016 Daniel

Daniel (view profile)

11 May 2016 Mahak SINGH CHAUHAN

Very nice !! Thanks

03 Apr 2013 Clara

Clara (view profile)

30 Jan 2013 onur

onur (view profile)


30 Jan 2013 onur

onur (view profile)

20 Sep 2012 Katarzyna Zarnowiec

20 Jun 2012 Christof Sailer

18 Oct 2011 Danielle

Great! Just what I was looking for!

14 Jul 2011 svetlana


02 Oct 2009 Chamane

Excellent code! Thanks

24 Jun 2009 Karl

Karl (view profile)

Thanks for the improvement, Thierry! The 'cellfun(@fcn, ...)' method, though elegant, unfortunately doesn't work on Matlab 6.x (which some of us still have to use. Sigh.)

Comment only
24 Jun 2009 Jan

Jan (view profile)

Very handy indeed. And nice tweak, Thierry

24 Jun 2009 Thierry Dalon

Good idea!
I prefer to use 0 for gcf or 'last' option.
I've modified the function like this:
function cab(varargin)
% CAB close all but
% This function closes all figures currently open EXCEPT for
% those listed as arguments. 'cab' stands for 'close all but'.
% CAB figure_handle1 figure_handle2 ... (command line syntax)
% CAB(figure_handle1, figure_handle2, ...)
% Use 0 for gcf. CAB 0 closes all figures except current one.
% CAB : same as close all
if nargin==0,
close all;

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

if iscellstr(varargin) % command line syntax
delete(setdiff(all_figs, figs2keep)

Comment only
22 Jun 2009 Aurelien Queffurust

A fantastic short code to save time ! Thanks for this submission!

19 Jun 2009 Michelle Hirsch

I love it. Not sure how I got along for 15 years without it! One small note - the "last" syntax actually keeps the current figure open, which wasn't necessarily the last one created. I love this behavior so much, that I tweaked CAB to do this with no input arguments, too.

Thanks for a simple, elegant, and very useful function.

15 Jun 2009 Itamar

Itamar (view profile)

Cool things. I love it. It is really useful to me as i work with a lot of figures open.

22 Jun 2009 1.1

Fixed 'last' option so it actually closes the last figure opened, rather than the current figure.

Contact us