导出Mathematica打印[]输出到.txt文件 [英] Exporting Mathematica Print[] Output to a .txt file

查看:475
本文介绍了导出Mathematica打印[]输出到.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的Mathematica笔记本,它定期使用 Print [] 命令来输出运行时信息。这是笔记本生成的唯一输出(除了导出的文件)。有没有什么办法可以自动将这个输出导出到.txt文件,而不必重写 Print [] 命令?

解决方案

根据 documentation Print 输出到 $ Output 这是一个流列表。所以,在笔记本的开始,

  strm = OpenWrite [output.log]; 
AppendTo [$ Output,strm];

在笔记本结尾处

 关闭[STRM]; 

请注意,如果执行在关闭流之前中断,那么您必须手动执行。此外,上面的代码将覆盖output.log中的先前数据,因此您可能希望使用 OpenAppend ,而不是

:为了保证 Abort 会被调用,可以考虑使用其中的一种技术>这里

I have a large Mathematica notebook that uses Print[] commands periodically to output runtime messages. This is the only output (aside from exported files) that this notebook generates. Is there any way I can automate the export of this output to a .txt file without having to re-write the Print[] commands?

解决方案

According to the documentation, Print outputs to the $Output channel which is a list of streams. So, at the beginning of the notebook,

strm = OpenWrite["output.log"];
AppendTo[ $Output, strm ];

and at the end of the notebook

Close[strm];

Note, if execution is interrupted prior to closing the stream, then you'll have to do it manually. Also, the above code will overwrite prior data in "output.log," so you may wish to use OpenAppend, instead.

Edit: to guarantee that Abort will be called, consider using one of the techniques outlined here.

这篇关于导出Mathematica打印[]输出到.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆