问题
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.
回答1:
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
回答2:
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.
回答3:
I just realized that you might not know that unlike MATLAB, Mathematica is designed to run as two separate processes: the Front End is the user interface, and lets you work with notebooks. The Kernel does the computations. You can quit the kernel without affecting the front end, or even start more than one kernel for different notebooks, or start a kernel on a remote computer and use it with a local front end.
I believe that the only reliable way to clean everything is to Quit
the kernel and re-start it (which is automatic). There are just too many things that can get modified apart from user variables/functions (including In
/Out
, loaded packages, system caches, etc.). So if you need a truly fresh start, I recommend Quit
.
For a "soft" reset, @yoda already mentioned ClearAll["Global`*"]
. There's the << Utilities`CleanSlate`
package, which automates a little bit more than this. You can read the package docs inside the AddOns\ExtraPackages\Utilities\CleanSlate.m
file.
In short, CleanSlate[]
will attempt to take you back to the kernel state when the package was loaded. ClearInOut[]
will clear In
and Out
to save memory.
I haven't used this package in years (except for the ClearInOut[]
functionality), as the Mathematica kernel starts up quickly on modern computers, so I just use Quit
. So I can't tell you how well it works.
来源:https://stackoverflow.com/questions/8260217/is-there-a-way-to-clear-everything-before-starting-in-mathematica