Code covered by the BSD License  

Highlights from
clearnot

Be the first to rate this file! 18 Downloads (last 30 days) File Size: 1.43 KB File ID: #39776

clearnot

by

 

CLEARNOT clears all variables except the input variables

| Watch this File

File Information
Description

CLEARNOT A B clears all the variables in the calling workspace except A and B (or any variable that is input to CLEARNOT).

Useful for cleaning up a cluttered workspace when you want to preserve a few variables.

The functional form of CLEARNOT is also valid: CLEARNOT('A','B').

TODO: make it work with wildcards.

Example:
    a=2;b=3;c=4;d=5;
    clearnot a c
This clears B and D but preserves A and C in the workspace.

MATLAB release MATLAB 7 (R14)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (5)
12 Mar 2013 Chad Greene

Oliver: I have used the keep function for years, but I have always thought it was poorly named. Matlab will always keep your variables unless you tell it to do something else with them. It took me years to find the keep function on FEX because I never searched for the word 'keep'. I still have the darndest time remembering the function name because my thought process is typically that I want to clear all variable except a few. In other words, clearing comes to mind first, and then I think about keeping. This function is intuitively named and can be found easily using Function Hints. That is, I can type "clear" and tab to find "clearnot".

10 Jan 2013 Oliver Woodford

Have you seen the submissions keep, keep2, keep3, keep4, keep5? They all do the same thing as this. In what way is this submission an improvement?

10 Jan 2013 Jan Simon

The built-in function Richard has mentioned should be preferred.

In addition the method can be simplified:
vars = setdiff(evalin('caller','who'), varargin);
evalin('caller', ['clear ', sprintf('%s ', vars{:})]);

09 Jan 2013 Richard Crozier

I should also point out clearvars already works with wildcards, and also regular expressions.

09 Jan 2013 Richard Crozier

how about the built-in function clearvars, with the -except flag. Your example would then be:

clearvars -except a c

Contact us