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?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityAn alternative that doesn't require protecting or using private contexts:
Clear @@ DeleteCases[Names@"`*", "b"];
To keep a few (as suggested in comments) replace "b"
with "b"|"g"|"h"
.
"b"
above with your variables separated by |
, like "b" | "c" | "d"
.
$\endgroup$
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.
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
special
variables?
$\endgroup$
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.
Also, preserve it in a "private" context prior to the cleaning:
b = 1;
myCtx`b = b;
Clear["Global`*"];
b = myCtx`b
(* 1 *)
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.