MATLAB: How to create a log of all the commands run by the MATLAB files

MATLAB

I am having problems locating the source of a problem with my program. I want to create a log file to show me what my program was doing at the time of the failure.

Best Answer

There are a couple MATLAB functions which will be helpful for determining where the crash is occurring in your code. These functions are DIARY and ECHO. The DIARY command logs all output to the command window into a file. The ECHO command echos commands from your MATLAB files to the command window.
To enable the highest level of logging execute the following commands before running your program:
echo on
echo on all
diary crashlog.txt
When the program finishes execution you can turn off the logging with the command:
diary off
The file crashlog.txt will contain all commands run by your program and any output displayed to the command window. If MATLAB crashes, the log will be abruptly terminated at the point where the crash occurred. Please note, the ECHO command will cause your application to run more slowly. Also, if your application runs for a long time, this may result in a very large log file. The diary command will continue to append to an existing file, so you may want to periodically delete this file. If you are not using any function files you can eliminate the command "echo on all" or if you only want to enable logging for only some function files you can enable echoing for only certain functions. For more information on these functions you can execute the following commands at the MATLAB prompt:
doc diary
doc echo