Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to clear everything before starting in Mathematica?

In MATLAB there is the function clear to delete all current variables. This is very useful if you start something totally new and don't want to get conflicts with earlier calculations. I'm searching something similar for Mathematica now, but I couldn't find anything except of Clear[VAR] which removes only the variable VAR.

like image 954
lumbric Avatar asked Nov 24 '11 16:11

lumbric


People also ask

How do you clear all in Mathematica?

To clear all definitions of quantities you've introduced in a Mathematica session so far, type: ClearAll["Global'*"].

What is the use of Clear All command in Mathematica?

clears all values, definitions, attributes, messages, and defaults associated with symbols. clears all symbols whose names textually match any of the formi.

How do you clear a Mathematica kernel?

Use the command Clear[“Global`*”] to clear all variables. Choose Quit Kernel from the Evaluation menu to quit the kernel, which clears all the defined variables.

What does blue text in Mathematica mean?

Once a variable is subjected to Clear , Remove or =. it will again be undefined and appear blue everywhere in the notebook, even at earlier points. But sometimes it does happen that Mathematica doesn't immediately recognize a previously defined symbol and incorrectly leaves it blue.


2 Answers

You can use ClearAll to clear the variables and their attributes in your Global context (default) like so:

ClearAll["Global`*"]

If you're working inside a different context (e.g., notebook specific context or cell group specific context), you can do

ClearAll[Evaluate[Context[] <> "*"]]

If you want to remove all symbols from the kernel so that Mathematica doesn't recognize them anymore, you can use Remove[] similar to the above two examples.

Barring these, you can always quit the kernel with Quit[] which will remove all symbols. A fresh kernel will be initiated the next time you evaluate something.

like image 145
abcd Avatar answered Jan 04 '23 03:01

abcd


I recommend one of two methods:

1. Keyboard shortcut to Quit[] the kernel

There is a system file KeyEventTranslations.tr that you can edit to customize keyboard shortcuts. I, as others, have added Ctrl+Q to Quit[] the kernel, allowing for a rapid clearing of all sessions variables. For more information on setting this up, see:

  • Customizing Mathematica shortcuts

2. Give the new Notebook a unique context

In Mathematica, the current $Context defines what Context unqualified symbol names belong to. By giving a new Notebook a unique Context, which is easily done through the Evaluation menu, the symbols used in that Notebook will not collide with unqualified symbols in other Notebooks. See the following question for more detailed information:

  • Mathematica - Separating Notebooks
like image 36
Mr.Wizard Avatar answered Jan 04 '23 03:01

Mr.Wizard