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.

 채택된 답변

Image Analyst
Image Analyst 2013년 4월 8일

0 개 추천

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

댓글 수: 2

carlos Uribe
carlos Uribe 2013년 4월 11일
This gives me an output to a file that I haven't been able to update on the GUI
Image Analyst
Image Analyst 2013년 4월 11일
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/

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Yair Altman
Yair Altman 2013년 4월 15일

6 개 추천

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:

댓글 수: 9

Luis Camilo
Luis Camilo 2014년 6월 10일
Yair, you have an exemple for this (.m)? I need show the command window in GUIDE that is compiled .exe.tanks
Aditya
Aditya 2014년 6월 16일
Hi Luis,
I'm facing the sam eproblem. I need to display command window output to a guide gui. Have you figured out a way to solve this?
mashor housh
mashor housh 2014년 8월 7일
Hi Adita and Luis,
In case you deal with compiled GUI you can compile it with console option enabled, then you can call it in batch file: myprgram.exe > output.txt the output file could be read from your GUI callback.
The above solution will create console window whenever you open the program. if you do not want the console window to appear then you may build another m-file (say start_myprgram.m) containing the following line "dos('myprogram.exe > output.txt')" you can compile start_myprgram.m as your main program file.
Chris
Chris 2014년 11월 19일
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.
Muhammad RSMY
Muhammad RSMY 2017년 8월 18일
I tried the code but unfortunately doesn't work, maybe I code it wrong, cause I am new in coding
Trevor Harris
Trevor Harris 2018년 8월 9일
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?
Hao Li
Hao Li 2020년 3월 13일
The same problem.
Before compiling, everything worked just fine.
After compiling, jCmdWin = jDesktop.getClient('Command Window') yeilds empty.
Image Analyst
Image Analyst 2020년 3월 14일
What version are you using? Did you compile with the -m option or the -e option? If the -e option, there is no console window so that would explain it. If -m then perhaps the compiled version does not consider the console window to be the "command" window.
Yair Altman
Yair Altman 2020년 3월 15일
편집: Yair Altman 2020년 3월 15일
Compiled applications that have a console use plain-text output, and do not send output to a Desktop window ("client") called "Command Window". Such a window is only available when you use the Matlab Desktop, and not in compiled apps (which do not display the Desktop).
The trick that I showed above uses Java code to query the text displayed in the "Command Window" client window. Compiled apps do not have such a window, so you can not query their text.
You need to programmatically redirect console output to your GUI using dedicated commands, and not rely on simple fprintf() commands.

댓글을 달려면 로그인하십시오.

crawler
crawler 2019년 2월 21일
편집: crawler 2019년 2월 21일

3 개 추천

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

댓글 수: 4

Prajwol Tamrakar
Prajwol Tamrakar 2021년 4월 14일
Excellent! Thank you!!
Michael
Michael 2026년 2월 3일
I appreciate that this this is pretty old, but I'm trying to accomplish the same...capture sent to the command window and display it in a text area (named jTextArea for simplicity) in an App Designer app (not compiled). The error I'm recieving is "Dot indexing is not supported for variables of this type." at the line "jTextArea = jCmdWin.getComponent(0).getViewport.getView;". I was prompted to refer to JTextArea as app.JTextArea, but doing to made no difference.
Many thanks for any assistance.
Walter Roberson
Walter Roberson 2026년 2월 3일
편집: Walter Roberson 2026년 2월 3일
Which MATLAB release are you using? Support changed in R2025a.
Michael
Michael 2026년 2월 4일
R2025b...thx.

댓글을 달려면 로그인하십시오.

Jack Chynoweth
Jack Chynoweth 2020년 7월 6일

0 개 추천

I have appiled this method, and got to work in the matlab enviroment.
I have now converted it to a standalone app, and it is catching an error.
is there a way to apply the method to standalone appilcations?

댓글 수: 1

Yair Altman
Yair Altman 2020년 7월 6일
편집: Yair Altman 2020년 7월 6일
As already noted above, accessing the command window text does not work in compiled applications. Read above for details

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품

질문:

2013년 4월 8일

댓글:

2026년 2월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by