有没有办法在开始 Mathematica 之前清除所有内容?
MATLAB 中有一个函数 clear 可以删除所有当前变量。如果您开始一些全新的事情并且不想与早期的计算发生冲突,这非常有用。我现在正在为 Mathematica 搜索类似的内容,但除了 Clear[VAR] 之外我找不到任何东西,它只删除变量VAR。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
ClearAll
清除Global
上下文(默认)中的变量及其属性,如下所示:如果您在不同的上下文中工作(例如,笔记本特定上下文)或单元组特定上下文),您可以执行
如果您想从内核中删除所有符号,以便 Mathematica 不再识别它们,您可以使用
Remove[]
与上面两个例子类似。除了这些之外,您始终可以使用 Quit[] 退出内核,这将删除所有符号。下次您评估某些内容时,将启动一个新的内核。
You can use
ClearAll
to clear the variables and their attributes in yourGlobal
context (default) like so:If you're working inside a different context (e.g., notebook specific context or cell group specific context), you can do
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.我推荐以下两种方法之一:
1. 退出[]内核的键盘快捷键
有一个系统文件
KeyEventTranslations.tr
,您可以编辑该文件来自定义键盘快捷键。我和其他人一样,将 Ctrl+Q 添加到Quit[]
内核中,以便快速清除所有会话变量。有关设置的更多信息,请参阅:2. 为新笔记本提供独特的上下文
在 Mathematica 中,当前
$Context
定义了 上下文非限定符号名称所属。通过为新笔记本提供唯一的上下文(可以通过Evaluation
菜单轻松完成),该笔记本中使用的符号将不会与其他笔记本中的不合格符号发生冲突。有关更多详细信息,请参阅以下问题: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 toQuit[]
the kernel, allowing for a rapid clearing of all sessions variables. For more information on setting this up, see: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 theEvaluation
menu, the symbols used in that Notebook will not collide with unqualified symbols in other Notebooks. See the following question for more detailed information:我刚刚意识到您可能不知道与 MATLAB 不同,Mathematica 被设计为作为两个独立的进程运行:前端是用户界面,让您可以使用笔记本进行工作。内核进行计算。您可以退出内核而不影响前端,甚至可以为不同的笔记本启动多个内核,或者在远程计算机上启动一个内核并将其与本地前端一起使用。
我相信清理所有内容的唯一可靠方法是退出内核并重新启动它(这是自动的)。除了用户变量/函数(包括
In
/Out
、加载的包、系统缓存等)之外,还有太多的东西可以修改。因此,如果您需要真正的全新开始,我建议退出
。对于“软”重置,@yoda 已经提到了
ClearAll["Global`*"]
。有<<; Utilities`CleanSlate`
包,它的自动化程度比这多一点。您可以阅读AddOns\ExtraPackages\Utilities\CleanSlate.m
文件中的包文档。简而言之,
CleanSlate[]
将尝试在加载包时将您带回内核状态。ClearInOut[]
将清除In
和Out
以节省内存。我已经很多年没有使用这个软件包了(除了
ClearInOut[]
功能),因为 Mathematica 内核在现代计算机上启动得很快,所以我只是使用Quit
。所以我无法告诉你它的效果如何。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 (includingIn
/Out
, loaded packages, system caches, etc.). So if you need a truly fresh start, I recommendQuit
.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 theAddOns\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 clearIn
andOut
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 useQuit
. So I can't tell you how well it works.