将输出错误消息保存到 MATLAB 中的文件

发布于 2024-08-14 19:41:27 字数 195 浏览 7 评论 0原文

有没有办法将 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 技术交流群。

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

发布评论

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

评论(5

荒路情人 2024-08-21 19:41:28
try
    % your code here
catch err
   fid = fopen('errorFile','a+');
   fprintf(fid, '%s', err.getReport('extended', 'hyperlinks','off'))
   fclose(fid)
end
try
    % your code here
catch err
   fid = fopen('errorFile','a+');
   fprintf(fid, '%s', err.getReport('extended', 'hyperlinks','off'))
   fclose(fid)
end
林空鹿饮溪 2024-08-21 19:41:28

对于像 Matlab 命令窗口那样的精确格式化样式,请使用:

rep = getReport(exception, 'extended', 'hyperlinks', 'off');

name = strcat('Data\', name, '.txt');
fid = fopen(name, 'w+t','n');
fprintf(fid, 'Error message\n-------------\n\n');
fprintf(fid, '%s\n', rep);
fclose('all');

For exact formatting style like from Matlab command window, use:

rep = getReport(exception, 'extended', 'hyperlinks', 'off');

name = strcat('Data\', name, '.txt');
fid = fopen(name, 'w+t','n');
fprintf(fid, 'Error message\n-------------\n\n');
fprintf(fid, '%s\n', rep);
fclose('all');
蓦然回首 2024-08-21 19:41:27

在代码周围使用 try...catch 语句。在catch块中,您可以写出包括堆栈信息的错误。使用sendmail,您甚至可以让代码通过邮件通知您错误(最好有一个弹出窗口,让用户决定是否要与您共享崩溃信息)

try
   % your code here
catch err
   %open file
   fid = fopen('logFile','a+');
   % write the error to file
   % first line: message
   fprintf(fid,'%s\n',err.message);

   % following lines: stack
   for e=1:length(err.stack)
      fprintf(fid,'%sin %s at %i\n',txt,err.stack(e).name,err.stack(e).line);
   end

   % close file
   fclose(fid)
end 

编辑为更明确地说明如何将错误消息写入文件

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)

try
   % your code here
catch err
   %open file
   fid = fopen('logFile','a+');
   % write the error to file
   % first line: message
   fprintf(fid,'%s\n',err.message);

   % following lines: stack
   for e=1:length(err.stack)
      fprintf(fid,'%sin %s at %i\n',txt,err.stack(e).name,err.stack(e).line);
   end

   % close file
   fclose(fid)
end 

Edited to be a bit more explicit on how to write error message to file

谁与争疯 2024-08-21 19:41:27

使用“diary”命令创建日志文件。这将使 Matlab 将所有命令行输出的副本写入文件,包括警告、错误消息以及未处理异常的堆栈跟踪。如果出现错误,Sendmail() 可以将其发送给您。如果要节省空间,可以让程序在正常(无错误)程序退出时删除其日志文件。

恕我直言,这比使用“try ... catch;写错误;结束”更可取,因为:

  • 它将捕获所有未捕获的错误,包括从 AWT 线程引发的 Java 异常以及来自 GUI 中的 M 代码回调的错误,这些错误可以是很难尝试/抓住。
  • 如果 Matlab 严重崩溃(例如出现段错误),则 M 代码级别的 try/catch 将无法捕获它。但日志文件可能仍然记录段错误转储。
  • 您可以发出进度消息、调试信息和警告,以提供有关导致错误的程序行为的更多信息,并且它们都会被捕获。
  • 我喜欢尽量减少 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:

  • It will capture all uncaught errors, including Java exceptions raised from the AWT thread and errors from M-code callbacks in your GUI, which can be hard to get try/catches around.
  • If Matlab is crashing hard, like with a segfault, the M-code level try/catch won't catch it. But the diary file may still record the segfault dump.
  • You can emit progress messages, debug info, and warnings to give more information on your program's behavior leading up to the errors, and they'll all be captured.
  • I like to keep code in catch blocks minimal.

There's also a command line option that does the equivalent; I don't know how to invoke that for compiled Matlab.

泪之魂 2024-08-21 19:41:27

对于旧版本的 MATLAB,您可以使用 LASTERROR函数来获取有关 MATLAB 最近发出的错误的信息。但是,此函数将在较新的 MATLAB 版本中逐步淘汰。

对于较新版本的 MATLAB,我建议使用 < strong>MException 类捕获错误信息。您可以使用 try-catch 块捕获 MException 对象 正如乔纳斯建议的,或者您可以使用静态 MException.last< /strong> 方法 获取最后一个未捕获的异常(取决于您运行代码的方式):

%# OPTION 1:
%# --------
try
  my_code();
catch ME
  %# Save data in ME to file
end
%# OPTION 2:
%# --------
my_code();
ME = MException.last;
%# Save data in ME to file

无论您采用哪种方式捕获 MException 对象,都可以使用 MException.getReport 方法 显示格式化消息字符串,包括 MException 中包含的信息对象:

msgString = getReport(ME,'basic');     %# Displays the higher level error
msgString = getReport(ME,'extended');  %# Displays the error and the stack

然后您可以将消息字符串写入文件。

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):

%# OPTION 1:
%# --------
try
  my_code();
catch ME
  %# Save data in ME to file
end
%# OPTION 2:
%# --------
my_code();
ME = MException.last;
%# Save data in ME to file

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:

msgString = getReport(ME,'basic');     %# Displays the higher level error
msgString = getReport(ME,'extended');  %# Displays the error and the stack

You can then write the message string to a file.

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