File Exchange

image thumbnail


version 1.0 (1.43 KB) by

CLEARNOT clears all variables except the input variables



View License

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.

Comments and Ratings (5)

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

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?

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

Richard Crozier

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

Richard Crozier

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

clearvars -except a c

MATLAB Release
MATLAB 7 (R14)
Tags Add Tags

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

» Watch video