This m-file allows you to export all text in the Command Window to a cell array of strings, each entry being a line of text from the window. The unparsed raw string is also available, if desired.
Operation is very simple - this file looks for the appropriate java object in the root window and gets text from it.
I was looking for a way to do this, particularly for error tracking in some software I maintain, and nobody seemed to have written up this method, so I present it here for you to use.
Hugh Nolan (2021). Command window text (https://www.mathworks.com/matlabcentral/fileexchange/31438-command-window-text), MATLAB Central File Exchange. Retrieved .
Yes, the com commands depend on the Matlab version. The posted code runs on 2009a.
Thanks Jan - that didn't work exactly as written (version differences? I'm running R2007b) but gave me enough to work it out - updated and faster. Good work!
A short method without FINDJOBJ:
cmdDoc = com.mathworks.mde.cmdwin.cmdWinDocument.getInstance;
Str = cmdDoc.getText(1, cmdDoc.getLength);
CStr = regexp(Str, '\n', 'split');
The methods cmdWin.findStringInContent and cmdWin.findStringInContentBackwards can be helpful also to restrict the exported string.
Strange effects are possibe with the insertString method: You can modify former output!
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!