File Exchange

image thumbnail

Close all figures except those listed

version 1.1.0.0 (1.46 KB) by Karl
Close all figures except those listed by the user.

12 Downloads

Updated 22 Jun 2009

View Version History

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

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'.

Cite As

Karl (2021). Close all figures except those listed (https://www.mathworks.com/matlabcentral/fileexchange/24420-close-all-figures-except-those-listed), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (26)

Miranda Nixon-Hill

Simon

choong

Dharmendra Sharma

Zoe

J K

Thanks! :D

Josh

Love it!

Tunggul Bhirawa

Very useful!

paula

Thank you!! :)

Fabrizio Schiano

It works smoothly! I was coding it but then I found this. You saved me 30 minutes :)

Daniel

Mahak SINGH CHAUHAN

Very nice !! Thanks

Clara

onur

THANKS

onur

Katarzyna Zarnowiec

Christof Sailer

Danielle

Great! Just what I was looking for!

svetlana

Cool!

Chamane

Excellent code! Thanks

Karl

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

Jan

Very handy indeed. And nice tweak, Thierry

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'.
%
% SYNTAX:
% 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;
return
end

% 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
figs2keep=cellfun(@str2double,varargin);
else
figs2keep=[varargin{:}];
end
figs2keep(figs2keep==0)=gcf;
delete(setdiff(all_figs, figs2keep)

Aurelien Queffurust

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

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.

Itamar

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

MATLAB Release Compatibility
Created with R2007b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

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

Start Hunting!