将输出错误消息保存到 MATLAB 中的文件
有没有办法将 MATLAB 错误消息保存到文件中?
这可能是一个简单的问题,但谷歌无法给我答案。我编译了一个 GUI 可执行文件,无需 MATLAB 许可证即可使用,但偶尔会冻结。出于美观目的,我隐藏了通常伴随此类可执行文件的命令窗口,因此我无法通过命令提示符获取错误消息。我希望能够创建一个错误日志,可以通过电子邮件发送给我进行调试。
谢谢!
Is there a way to save MATLAB error messages to a file?
This may be a simple issue, but Google couldn't give me an answer. I've compiled a GUI executable for use without a MATLAB license, and occasionally it freezes. For aesthetic purposes, I suppressed the command window normally accompanying such an executable, so I can't get an error message out through the command prompt. I'd like to be able to create an error log which can be emailed to me for debugging.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于像 Matlab 命令窗口那样的精确格式化样式,请使用:
For exact formatting style like from Matlab command window, use:
在代码周围使用 try...catch 语句。在catch块中,您可以写出包括堆栈信息的错误。使用sendmail,您甚至可以让代码通过邮件通知您错误(最好有一个弹出窗口,让用户决定是否要与您共享崩溃信息)
编辑为更明确地说明如何将错误消息写入文件
Use try...catch statements around the code. In the catch block, you can write out the error including stack information. Using sendmail, you can even have the code notify you of errors by mail (ideally with a popup that lets users decide whether they want to share the crash information with you)
Edited to be a bit more explicit on how to write error message to file
使用“diary”命令创建日志文件。这将使 Matlab 将所有命令行输出的副本写入文件,包括警告、错误消息以及未处理异常的堆栈跟踪。如果出现错误,Sendmail() 可以将其发送给您。如果要节省空间,可以让程序在正常(无错误)程序退出时删除其日志文件。
恕我直言,这比使用“try ... catch;写错误;结束”更可取,因为:
还有一个命令行选项可以执行相同的操作;我不知道如何为编译后的 Matlab 调用它。
Use the "diary" command to create a log file. This will make Matlab write a copy of all the command line output to a file, including warnings, error messages, and the stack traces for unhandled exceptions. Sendmail() can then send it to you on errors. If you want to save space, you can have the program delete its log file on a normal (no error) program exit.
IMHO this is preferable to using the "try ... catch; write errors; end" because:
There's also a command line option that does the equivalent; I don't know how to invoke that for compiled Matlab.
对于旧版本的 MATLAB,您可以使用 LASTERROR函数来获取有关 MATLAB 最近发出的错误的信息。但是,此函数将在较新的 MATLAB 版本中逐步淘汰。
对于较新版本的 MATLAB,我建议使用 < strong>MException 类 到 捕获错误信息。您可以使用 try-catch 块捕获 MException 对象 正如乔纳斯建议的,或者您可以使用静态 MException.last< /strong> 方法 获取最后一个未捕获的异常(取决于您运行代码的方式):
无论您采用哪种方式捕获 MException 对象,都可以使用 MException.getReport 方法 显示格式化消息字符串,包括 MException 中包含的信息对象:
然后您可以将消息字符串写入文件。
For older versions of MATLAB you can use the LASTERROR function to get information about the most recent error issued by MATLAB. However, this function will be phased out in newer MATLAB versions.
For newer versions of MATLAB, I would suggest making use of the MException class to capture error information. You can catch an MException object using a try-catch block as Jonas suggested, or you could potentially use the static MException.last method to get the last uncaught exception (depending on how you run your code):
Whichever way you capture the MException object, you can use the MException.getReport method to display a formatted message string including the information contained in the MException object:
You can then write the message string to a file.