5.0

5.0 | 5 ratings Rate this file 55 downloads (last 30 days) File Size: 1.46 KB File ID: #24420

Close all figures except those listed

by Karl

 

11 Jun 2009 (Updated 22 Jun 2009)

Code covered by BSD License  

Close all figures except those listed by the user.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

Download Now | Watch this File

File Information
Description

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

MATLAB release MATLAB 7.5 (R2007b)
Zip File Content  
Other Files cab.m,
license.txt
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (7)
15 Jun 2009 Itamar

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

19 Jun 2009 Scott 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.

22 Jun 2009 Aurelien Queffurust

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

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

24 Jun 2009 Jan

Very handy indeed. And nice tweak, Thierry

24 Jun 2009 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.)

02 Oct 2009 Chamane

Excellent code! Thanks

Please login to add a comment or rating.
Updates
22 Jun 2009

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

Tag Activity for this File
Tag Applied By Date/Time
gui Karl 12 Jun 2009 09:58:26
figure Karl 12 Jun 2009 09:58:27
figure J.R.! Menzinger 22 Jun 2009 09:43:20
figure checker i 24 Aug 2009 17:33:15
gui checker i 24 Aug 2009 17:33:16
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com