MATLAB: Copy Matlab Command Window without diary

command windowdiary

Hi everyone,
I would like to add an optional function command at the end of a script that logs the command window to a file.
I know that diary gets the job done, but diary must be activated at the beginning of the execution, and this is not always possible.
I also know that the diary command can be put in the startup.m file so that it is automatically started with the Matlab session, but also this option is not always a feasible solution (I don't have control of the computer where the code is run).
There can be also many other objections for my intention to not using diary, but let's assume that I am at least curious: is it possible to mimic the "select all"+"copy" sequence to get all the text that is available in the command window at a specific moment? the output can be stored in a variable or file, it is not important.
Thank you for your help
Fabio

Best Answer

Hi Fabio,
I Hope that you have solved your problem already, in case if someone else might be interested in this, here's the solution.
After a bit of digging around, I managed to find a way to get the text from the command window programatically
desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
cmdWindow = desktop.getClient('Command Window');
cmdWindowScrollPane = cmdWindow.getComponent(0);
cmdWindowScrollPaneViewport = cmdWindowScrollPane.getComponent(0);
cmdTextUI = cmdWindowScrollPaneViewport.getComponent(0);
cmdText = cmdTextUI.getText;