MATLAB Answers

0

Command Window Output to GUI

Asked by carlos Uribe on 8 Apr 2013
Latest activity Edited by Shakirudeen Lasisi on 21 Feb 2019
Hello everyone,
I'm creating a GUI and at some point of it I need to run some cshells that begin printing many notifications in the command window. This all works fine, however, it would be great if all that output can be printed in the GUI itself. Is there a way of doing this? Sort of like cloning the command window into a "Static Text" field or similar in the GUI.
Any help is appreciated.

  0 Comments

Sign in to comment.

3 Answers

Answer by Image Analyst
on 8 Apr 2013
 Accepted Answer

See if diary() or echo() can help you do what you want.

  2 Comments

This gives me an output to a file that I haven't been able to update on the GUI
Image Analyst
on 11 Apr 2013
Then you may have to change the functions that are spewing the stuff to the command window to return the text in a string instead. If it just sort of freely spews text out asynchronously to the command window, then you may be out of luck.
Try Yair Altman's site: http://undocumentedmatlab.com/

Sign in to comment.


Answer by Yair Altman on 15 Apr 2013

You can access the CW text programmatically as follows:
jDesktop = com.mathworks.mde.desk.MLDesktop.getInstance;
jCmdWin = jDesktop.getClient('Command Window');
jTextArea = jCmdWin.getComponent(0).getViewport.getView;
cwText = char(jTextArea.getText);
You can place a callback that will update your GUI whenever new text is added to the CW:
set(jTextArea,'CaretUpdateCallback',@myUpdateFcn)
More details on how the CW works internally can be found in:

  6 Comments

Chris
on 19 Nov 2014
Yair,
Do you know how this would be accomplished for a "MATLAB Command Window" using the -nodesktop mode? I'm trying to run an invisible automation server and capture the command output as part of the GUI, but am unsure of how to access the text in the Command Window.
I tried the code but unfortunately doesn't work, maybe I code it wrong, cause I am new in coding
This doesn't seem to function for a GUI that I'm trying to compile. I'm using 2017b.
When I first compiled I got an error at the "jTextArea = jCmdWin.getComponent(0).getViewport.getView;", saying I was asking for a reference from a non-struct object.
I then re-complied not suppressing the output from the jDesktop and jCmdWin lines. The jDesktop line seems to run fine and I get a com.mathwors.mde.desk.MLDesktop@127a7a2e... but the jCmdWin line yields an empty double. Any idea what may be going on? Has matlab changed this functionality since this post?

Sign in to comment.


Answer by Shakirudeen Lasisi on 21 Feb 2019
Edited by Shakirudeen Lasisi on 21 Feb 2019

To use the method by @Yair Altman, you can make the intialisations (setting callbacks etc..) at the startp function of the window containing the console e.g.
function startupFcn(app)
try
jDesktop = com.mathworks.mde.desk.MLDesktop.getInstance;
jCmdWin = jDesktop.getClient('Command Window');
jTextArea = jCmdWin.getComponent(0).getViewport.getView;
set(jTextArea,'CaretUpdateCallback',@app.setPromptFcn)
catch
warndlg('fatal error');
end
end
You can add a textarea at the window to display the console text.
Then you can make a private function for the event handler as such:
methods (Access = private)
function setPromptFcn(app,jTextArea,eventData,newPrompt)
% Prevent overlapping reentry due to prompt replacement
persistent inProgress
if isempty(inProgress)
inProgress = 1; %#ok unused
else
return;
end
try
% *** Prompt modification code goes here ***
cwText = char(jTextArea.getText);
app.TextArea.Value = cwText;
% force prompt-change callback to fizzle-out...
pause(0.02);
catch
% Never mind - ignore errors...
end
% Enable new callbacks now that the prompt has been modified
inProgress = [];
end % setPromptFcn
end
Writing the callback function tis way prevents entering an endless loop.
You can write whatever you like between the try statement here, to change how the callback behaves

  0 Comments

Sign in to comment.