MATLAB: How to programatically print out the contents of the MATLAB Command Window

commanddiarylogMATLABprintwindow

The contents of MATLAB's Command Window can be printed out by using 'Ctrl + P' or right-clicking and selecting "Print…" from the Command Window itself. How can I do the same programmatically, such as from within scripts?

Best Answer

This functionality can be emulated by using the "diary" and "system" commands. First, use "diary" to enable Command Window logging and save it in a file:
 
>> diary('commandLog')
>> diary on
This creates a file named 'commandLog' in MATLAB's current folder. Now, execute the commands of interest. Be sure to not suppress the output of variables with semicolons if you would like them to be written to the file. Once you are ready to print the file, execute the following commands to turn off diary logging, print the text file through Notepad, and then delete the file:
 
>> diary off
>> system('notepad /p commandLog')
>> delete('commandLog')
It is recommended that the file be deleted every time this workaround is used since "diary" appends Command Window text to the file if it already exists.