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?
|
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
Is there any command which clears all variables except a chosen one? |
||||
|
|
|
An alternative that doesn't require protecting or using private contexts:
|
|||||
|
|
You may want to place your special variable(s) in a separate context and then only clear
|
|||
|
|
|
The solution is to first protect
By protecting 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. |
|||
|
|
|
What I would to is quite simple: simply
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:
So that, for example
or
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:
|
|||
|
|
|
One other alternative is to |
|||
|
|