Command Window Output to GUI
84 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
carlos Uribe
le 8 Avr 2013
Commenté : Prajwol Tamrakar
le 14 Avr 2021
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 commentaires
Réponse acceptée
Image Analyst
le 8 Avr 2013
See if diary() or echo() can help you do what you want.
2 commentaires
Image Analyst
le 11 Avr 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.
Plus de réponses (3)
Yair Altman
le 15 Avr 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:
9 commentaires
Image Analyst
le 14 Mar 2020
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
le 15 Mar 2020
Modifié(e) : Yair Altman
le 15 Mar 2020
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
le 21 Fév 2019
Modifié(e) : crawler
le 21 Fév 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
Jack Chynoweth
le 6 Juil 2020
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 commentaire
Yair Altman
le 6 Juil 2020
Modifié(e) : Yair Altman
le 6 Juil 2020
As already noted above, accessing the command window text does not work in compiled applications. Read above for details
Voir également
Catégories
En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!