MATLAB: How to save command window output to a text file

MATLAB

How do we save command window output to a text file? The customer does not want to use fprintf function to write data to text file.

Best Answer

We can use the "diary" function: https://www.mathworks.com/help/matlab/ref/diary.html?s_tid=doc_ta
It will create a log file that contains keyboard input and result output. We could also customize the name for the output file.
Please note that the content in the command window might not be written into the log file right away. We can force the writing by toggling the switch for "diary" function. Example code:
diary on
a = 2 % Output of this line might not be written to file right away...
diary off % until this command is executed
diary on % You could put diary 'on' if you want more record of the command window
a = 3
diary off