Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a lot of variables: $a,b,c,d, \ldots $ I want to clear the values of all of them except $b$. It's really lengthy to write

Clear[a]; 
Clear[c];
...

Is there any command which clears all variables except a chosen one?

share|improve this question

migrated from math.stackexchange.com Sep 23 '12 at 3:44

This question came from our site for people studying math at any level and professionals in related fields.

6 Answers 6

An alternative that doesn't require protecting or using private contexts:

Clear @@ DeleteCases[Names@"`*", "b"];
share|improve this answer
    
Nice one, frog +1. What about the Timings? No impact? –  belisarius Sep 23 '12 at 8:38
    
@belisarius Timings? I don't really see a reason for there to be a significant difference in timings... –  rm -rf Sep 23 '12 at 16:25
    
@rm -rf, I was wondering how to use this technique if there are more than one variables? –  preeti Jan 21 at 2:38
    
@preeti You mean you want to delete all variables except a few specific ones? You can replace "b" above with your variables separated by |, like "b" | "c" | "d". –  rm -rf Jan 21 at 2:59
    
Excellent. If you could include that in your answer, it will make it easy for searching.Thanks. –  preeti Jan 21 at 4:08

You may want to place your special variable(s) in a separate context and then only clear Global`*. This is not the same as belisarius' recommendation. Instead create the variable b in a different context to begin with and append this context to the context path:

AppendTo[$ContextPath, "special`"];

special`b = 5; (* fully qualified name on first use *)

a = 1;
c = 2;

a + b + c
8
Clear["Global`*"]

a + b + c
5 + a + c
share|improve this answer

The solution is to first protect b, then clear all variables in current context, and then unprotect b again:

Protect[b];Clear[Evaluate[$Context<>"*"]];Unprotect[b]

By protecting b, you prevent it from getting cleared (Mathematica will complain, but do the right thing anyway). Note that the $Context construction makes sure that you clear the correct symbols if you have e.g. chosen to have an unique context for your notebook.

Also note that if you have defined variables in other contexts, too (e.g. using packages) which you also want to have cleared, you'll have to explicitly clear those contexts as well.

share|improve this answer

What I would to is quite simple: simply Block the variable you don't want to clear:

a=1;b=2;c=3;
Block[{a},
   Clear[a,b,c]
]

{a, b, c}

(* {1, b, c}  *)

This solution allows you to deal with symbols rather than their string names. You can get fancier and create a closure which would encapsulate your original list of symbols and take those symbols which should not be cleared:

ClearAll[makeClearF];
SetAttributes[makeClearF, HoldAll];
makeClearF[syms__Symbol] :=
     Function[Null, Block[{##}, Clear[syms]], HoldAll];

So that, for example

clear = makeClearF[a, b, c];

a=1;b=2;c=3;
clear[a]; (* clear all but a *)
{a,b,c}

(* {1,b,c}  *)

or

a=1;b=2;c=3;
clear[b,c]; (* clear all but b and c *)
{a,b,c}

(*  {a,2,3}  *)

I want to stress that this solution is different from those based on string names in that parsing is not involved. Which one is a better fit depends on the circumstances.

share|improve this answer

Also, preserve it in a "private" context prior to the cleaning:

b = 1;
myCtx`b = b;
Clear["Global`*"];
b = myCtx`b
(* 1 *)
share|improve this answer

One other alternative is to Compress[b] and copy the resulting string to your clipboard, then do the global clear or kernel quit, and then b=Uncompress["..."]. This is not the most elegant solution, obviously, but it's a useful technique to be aware of.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.