有没有办法在开始 Mathematica 之前清除所有内容?

发布于 2024-12-17 18:29:30 字数 161 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

栩栩如生 2024-12-24 18:29:30

您可以使用 ClearAll 清除 Global 上下文(默认)中的变量及其属性,如下所示:

ClearAll["Global`*"]

如果您在不同的上下文中工作(例如,笔记本特定上下文)或单元组特定上下文),您可以执行

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

如果您想从内核中删除所有符号,以便 Mathematica 不再识别它们,您可以使用 Remove[]与上面两个例子类似。

除了这些之外,您始终可以使用 Quit[] 退出内核,这将删除所有符号。下次您评估某些内容时,将启动一个新的内核。

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.

我是男神闪亮亮 2024-12-24 18:29:30

我推荐以下两种方法之一:

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 to Quit[] 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 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:

盛夏已如深秋| 2024-12-24 18:29:30

我刚刚意识到您可能不知道与 MATLAB 不同,Mathematica 被设计为作为两个独立的进程运行:前端是用户界面,让您可以使用笔记本进行工作。内核进行计算。您可以退出内核而不影响前端,甚至可以为不同的笔记本启动多个内核,或者在远程计算机上启动一个内核并将其与本地前端一起使用。


我相信清理所有内容的唯一可靠方法是退出内核并重新启动它(这是自动的)。除了用户变量/函数(包括In/Out、加载的包、系统缓存等)之外,还有太多的东西可以修改。因此,如果您需要真正的全新开始,我建议退出

对于“软”重置,@yoda 已经提到了 ClearAll["Global`*"]。有 <<; Utilities`CleanSlate` 包,它的自动化程度比这多一点。您可以阅读 AddOns\ExtraPackages\Utilities\CleanSlate.m 文件中的包文档。

简而言之,CleanSlate[] 将尝试在加载包时将您带回内核状态。 ClearInOut[] 将清除 InOut 以节省内存。

我已经很多年没有使用这个软件包了(除了 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 (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文