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

前端 未结 3 1953
面向向阳花
面向向阳花 2021-02-04 06:03

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 earl

相关标签:
3条回答
  • 2021-02-04 06:39

    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.

    0 讨论(0)
  • 2021-02-04 06:40

    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
    0 讨论(0)
  • 2021-02-04 07:04

    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.

    0 讨论(0)
提交回复
热议问题