Copy Matlab Command Window without diary

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

2 Comments

publish(...) perhaps?
How can I supply the command window lines as input for publish?

Sign in to comment.

 Accepted 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;

4 Comments

This is undocumented and so potentially subject to change without warning in future releases. It also has a problem that's common to all "after-the-fact" recordings of the text displayed in the Command Window: the buffer is finite. If your code writes a LOT of text to the Command Window, you're not going to capture it all. For example, I changed my preferences to limit the scroll buffer to 1000 and ran this code.
more off
Q = reshape(1:5500, 1100, 5) % no semicolon!
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;
S = string(cmdText);
more on
S
Here's the first part of S. Note that it doesn't start with the first row, which would start with 1.
S =
" 76 1176 2276 3376 4476
77 1177 2277 3377 4477
78 1178 2278 3378 4478
79 1179 2279 3379 4479
80 1180 2280 3380 4480
81 1181 2281 3381 4481
It'll definitely change with future releases as Java will be removed.
Alright, now I can see that there's an issue with the buffer, one could easily get around it with using the method getlinecount.
cmdTextUI.getLineCount
It's possible to clear command window and append text to variable, every, let's say 500 lines (it looks like limit is around 5000 lines), including the end. I am aware that this is not really a robust solution nor a proper one, but without diary, it could be barely sufficient.
Even though it may be not the cleanest way, especially for future releases, at the moment it is the only answer that solves exactly my problem. As soon as it won't work anymore, I'll contact Mario for new suggestions!
Thank you for your help
You're welcome, I am just returning the favor.

Sign in to comment.

More Answers (1)

I don't think there's a way to programmatically get access to the scroll buffer of the Command Window after-the-fact. You could try "clicking" and "typing" Ctrl-A and Ctrl-C in the Command Window using java.awt.Robot then use the clipboard function to paste that into a variable, but I'm not sure how practical that would be for your application.
If you can control how MATLAB gets launched, however, you could add the -logfile startup option to that command. This wouldn't require changing any files (like startup.m.)
You could capture everything under your control except the first line by calling diary as the first line in the initial function that gets called to start your analysis.
If you don't need to see the output as the code runs and you're using it only to capture that output into a variable (you're not trying to create dynamically named variables or the like) you could use evalc. But again, that's not an after-the-fact thing; you'd have to wrap your code (say the main function call) in evalc before or as you start running it.

1 Comment

Steve,
I tried to follow your suggestions, but the function is still not working properly, apparently in a non-deterministic way (ok, I don't believe that the behavior is random, just to say that I cannot isolate the problem). In addition the behavior is different with different OS.
I attach the function I wrote and the way I call it. Hopefully someone may have some hints
Thank you in advance
Fabio

Sign in to comment.

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!