14

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!

5 Answers 5

13

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

2
12

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.

2
  • Thanks, I'll give this a try as well if I have time. I've already got an adapted version of Jonas' code working.
    – Doresoom
    Dec 17, 2009 at 21:41
  • I personally favour using my own logfile (my classes all come with a method called 'log'), because I don't want to clutter the command window with all my debug info. However, for hard crashes and java exceptions, the diary is certainly very useful.
    – Jonas
    Dec 19, 2009 at 17:44
3

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.

2
try
    % your code here
catch err
   fid = fopen('errorFile','a+');
   fprintf(fid, '%s', err.getReport('extended', 'hyperlinks','off'))
   fclose(fid)
end
1

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');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.