Search code examples
matlaberror-reporting

Save output error messages to file in 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!


Solution

  • 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.