View License

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

» Watch video

Highlights from

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



Andrew Bliss (view profile)


CLEARNOT clears all variables except the input variables

| Watch this File

File Information

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.

    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

Chad Greene (view profile)

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

Comment only
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?

Comment only
10 Jan 2013 Jan Simon

Jan Simon (view profile)

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{:})]);

Comment only
09 Jan 2013 Richard Crozier

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

Comment only
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

Comment only

Contact us