mathematica 从命令行启动前端和 eval 笔记本

发布于 2024-12-05 04:31:08 字数 444 浏览 1 评论 0原文

有没有办法从(Windows)命令提示符启动mathematica前端(GUI)并让它评估笔记本而无需进一步的用户操作?

即使 mathematica.exe 采用 -run 和 -initfile 选项,它们的工作方式也不同 就像他们对 math.exe 所做的那样。 (例如,-run ''<


谢谢。第一个答案看起来很有希望,但是我得到了 FrontEndObject::不可用 前端不可用

(顺便说一下,根据文档,它是“UseFrontEnd”。)

也许是路径问题,但是即使在设置 $FrontEndLaunchCommand 之后也没有什么乐趣。

回复:初始化单元 - 这个简单的答案似乎完全符合我的要求需要排除“你想运行初始化吗..”提示框。如果有一个选项可以自动启动内核并运行初始化单元,那么了解这一点将非常有用。

顺便说一句,我正在运行6.0。

Is there a way to start up a mathematica front end (GUI) from a (Windows) command prompt and have it eval a notebook without further user action?

even though mathematica.exe takes the -run and -initfile options they dont work the same
as they do with math.exe. (-run ''<<file.m'' wants to open a file named ''<<file.m'' for example)


Thanks. The first answer looks promising, however I get
FrontEndObject::notavail
A front end is not available

(per docs it is "UseFrontEnd" by the way.)

Perhaps a path issue, however even after setting $FrontEndLaunchCommand no joy..

Re: Initialization Cell -- that simple answer would seem to do exactly what I need excepting for the "do you want to run initialization.." nag box. If there is an option somplace to automatically start a kernel and run initialization cells that would be really useful to know.

I'm running 6.0 by the way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

同尘 2024-12-12 04:31:08

您可以尝试以下操作:

在 C:\Program Files\Wolfram Research\Mathematica\7.0 中创建一个名为 firstgo.m 的文件,其中包含:

UsingFrontEnd[Module[{},
file = "C:\\Temp\\Test.nb";
targetnotebook = NotebookOpen[file, Visible -> True];
SelectionMove[targetnotebook, Next, Cell];
SelectionEvaluate[targetnotebook];
NotebookSave[targetnotebook];
NotebookClose[targetnotebook];
]];

在 C:\Temp 中创建一个名为 Test.nb 的文件,其中包含:

Module[{x1=0},
Export["C:\\Program Files\\Wolfram Research\\Mathematica\\7.0\\sin.gif",
Plot[Sin[x],{x,0,6}]];
While[x1<1000000,
If[Mod[x1,100000]==0,Print["x1="<>ToString[x1]]];
x1++]]

然后在 Windows 命令控制台中运行以下命令:

cd C:\Program Files\Wolfram Research\Mathematica\7.0
MathKernel -noprompt -initfile firstgo.m

您将看到 Test.nb 在 Mathematica 目录中创建了一个名为“sin.gif”的文件。 Test.nb 还包含一些打印输出,但尽管在前端运行并在运行后保存,但没有保存打印输出。另外,我还没有找到一种方法来退出内核而不中断前端进程。

附录

如果您知道您的过程将花费多长时间,您可以在完成后使用批处理文件关闭 Mathematica(为下一次运行做好准备)。此示例在关闭 Mathematica 之前暂停 20 秒。请注意,出于演示目的,firstgo.m 现在已移至 C:\Temp。在“我的文档”中创建一个批处理文件 RunFirstGo.bat,其中包含:

@echo off
setlocal
PATH = C:\Program Files\Wolfram Research\Mathematica\7.0\;%PATH%
echo Launching MathKernel %TIME%
start MathKernel -noprompt -initfile "C:\Temp\firstgo.m"
ping localhost -n 20 > nul
echo Terminating MathKernel %TIME%
taskkill /F /FI "IMAGENAME eq MathKernel.exe" > nul
endlocal

然后可以从 Windows 命令控制台运行 RunFirstGo.bat,如下所示:

cd my documents
runfirstgo

或者,可以将 RunFirstGo.bat 作为计划任务运行(通过 Windows 控制面板)。

You can try this:

In C:\Program Files\Wolfram Research\Mathematica\7.0 create a file called firstgo.m containing:

UsingFrontEnd[Module[{},
file = "C:\\Temp\\Test.nb";
targetnotebook = NotebookOpen[file, Visible -> True];
SelectionMove[targetnotebook, Next, Cell];
SelectionEvaluate[targetnotebook];
NotebookSave[targetnotebook];
NotebookClose[targetnotebook];
]];

And in C:\Temp create a file called Test.nb containing:

Module[{x1=0},
Export["C:\\Program Files\\Wolfram Research\\Mathematica\\7.0\\sin.gif",
Plot[Sin[x],{x,0,6}]];
While[x1<1000000,
If[Mod[x1,100000]==0,Print["x1="<>ToString[x1]]];
x1++]]

Then in a Windows command console run this:

cd C:\Program Files\Wolfram Research\Mathematica\7.0
MathKernel -noprompt -initfile firstgo.m

You will see the Test.nb creates a file called 'sin.gif' in the Mathematica directory. Test.nb also contains some Print output, but despite running in the front end and saving after the run there is no print output saved. Also, I have yet to figure out a way to quit the kernel without interrupting the front end process.

Addendum

If you know how long your process is going to take you can use a batch file to close Mathematica when it's done, (ready for the next run). This example pauses 20 seconds before shutting down Mathematica. Note, firstgo.m is now moved to C:\Temp for purpose of demonstration. Create a batch file RunFirstGo.bat in My Documents containing:

@echo off
setlocal
PATH = C:\Program Files\Wolfram Research\Mathematica\7.0\;%PATH%
echo Launching MathKernel %TIME%
start MathKernel -noprompt -initfile "C:\Temp\firstgo.m"
ping localhost -n 20 > nul
echo Terminating MathKernel %TIME%
taskkill /F /FI "IMAGENAME eq MathKernel.exe" > nul
endlocal

RunFirstGo.bat can then be run from a Windows command console like so:

cd my documents
runfirstgo

Alternatively, RunFirstGo.bat can be run as Scheduled Task (via Windows Control Panel).

川水往事 2024-12-12 04:31:08

啊哈...!!

Needs["JLink`"]; 
$FrontEndLaunchCommand="C:\\Program Files\\Wolfram Research\\Mathematica\\6.0\\Mathematica.exe";
ConnectToFrontEnd[];
UseFrontEnd[Module[{}, ...

跟进..为了完整性 --- 上面的 $FrontEndLaunchCommand 会导致 GUI 出现,以便您可以观察评估(我想要的)。默认情况下是在后台服务器模式下运行前端,因此您可以访问前端功能,但看不到它运行。

Aha...!!

Needs["JLink`"]; 
$FrontEndLaunchCommand="C:\\Program Files\\Wolfram Research\\Mathematica\\6.0\\Mathematica.exe";
ConnectToFrontEnd[];
UseFrontEnd[Module[{}, ...

follow up..for completeness --- the above $FrontEndLaunchCommand causes the GUI to come up so you can observe the evaluation (What I wanted). The default is to run a front end in a background server mode, so you have acess to front end functions but cant see it run.

自找没趣 2024-12-12 04:31:08

进行评估的不是前端,而是内核。您可以使用 MathematicaScript 来自动化它。

It is not the FrontEnd that does the evaluation but the Kernel. You can use MathematicaScript to automate it.

琉璃梦幻 2024-12-12 04:31:08

如果这是针对特定笔记本,您是否尝试将笔记本中的相关单元设置为具有属性 初始化单元格?或者您想使用任何笔记本电脑来执行此操作吗?如果是后者,Sjoerd 的答案效果更好。

If this is for a specific notebook, have you tried setting the relevant cells in the notebook to have the property InitializationCell? Or do you want to do this with any notebook? If the latter, Sjoerd's answer works better.

瞎闹 2024-12-12 04:31:08

我不知道该主题是否仍然活跃,但是对于那些因“初始化单元警告”问题而停止的人,您可以将其更改为“Flase”

选项检查器 ->笔记本选项 ->评估选项 ->初始化单元警告 ->False

I don't know if the topic is still active, but for those who are stopped at the "initialization cell warning" problem, you can change this to "Flase" in the

Option Inspector -> Notebook Options -> Evaluation Options -> InitializationCellWarning ->False

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