Redirecting console messages to GUI

9 views (last 30 days)
Muhammad Hammad
Muhammad Hammad on 21 Mar 2017
Commented: Muhammad Hammad on 23 Mar 2017
Hi all,
I am trying to redirect Matlab console output in a separate panel, by using the following file https://www.mathworks.com/matlabcentral/fileexchange/58431-gui-mirrorcmdwindow/content/gui_MirrorCmdWindow.m gui_MirrorCmdWindow
However, I have done minor updatings like passing "mycommand" parameter to execute the exe file and show its console messages in real time, showing "Ok" button, and make the console window size larger.
The code is as follows:
%code
function [] = gui_MirrorCmdWindow(mycommand)
h.f = figure('Position',[810 300 500 300],'tag','figuremirror');
fig1=gcf;
fig1.ToolBar = 'none';
fig1.MenuBar = 'none';
fig1.NumberTitle = 'off';
h.txtOut = uicontrol(h.f,'Style','edit','Max',10000,'Min',0,...
'HorizontalAlignment', 'left','FontName','FixedWidth',...
'Units','Normalized', 'Enable','inactive',...
'Position',[.05 .2 .9 .75], 'tag','textout');
h.btnPing = uicontrol(h.f,'Style','Pushbutton', 'String','Ok', 'Units',...
'Normalized','Position',[.05 .05 .9 .1], 'Callback',@myCloseRequestFcn);
guidata(h.f,h)
%// intercept close request function to cleanup before close
set(gcf,'CloseRequestFcn',@myCloseRequestFcn)
%%// Get the handle of the Matlab control window
jDesktop = com.mathworks.mde.desk.MLDesktop.getInstance;
jCmdWin = jDesktop.getClient('Command Window');
jTextArea = jCmdWin.getComponent(0).getViewport.getView;
cwText = char(jTextArea.getText);
%%// Get the handle of the jave edit box panel component
jtxtBox = findjobj(h.txtOut) ;
jTxtPane = jtxtBox.getComponent(0).getComponent(0) ;
%// Save these handles
setappdata( h.f , 'jTextArea', jTextArea ) ;
setappdata( h.f , 'jTxtPane', jTxtPane ) ;
tab
function tab
getappdata(h.f) ;
jTextArea = getappdata( h.f , 'jTextArea' ) ;
startPos = jTextArea.getCaretPosition ;
set(jTextArea,'CaretUpdateCallback',{@commandWindowMirror,h.f,startPos}) ;
pause(1);
system(mycommand);
end
function commandWindowMirror(~,~,hf,startPos)
h = guidata(gcf) ;
if isstruct(h)
jTextArea = getappdata( h.f , 'jTextArea' ) ;
%// retrieve the text since the start position
txtLength = jTextArea.getCaretPosition-startPos ;
%// in case a smart bugger pulled a 'clc' between calls
if txtLength > 0
cwText = char(jTextArea.getText(startPos-1,txtLength) ) ;
end
%// display it in the gui textbox
set( h.txtOut, 'String',cwText ) ;
jtxtBox = findjobj(h.txtOut) ;
jTxtPane = jtxtBox.getComponent(0).getComponent(0) ;
jTxtPane.setCaretPosition(jTxtPane.getDocument.getLength);
end
end
function scroll_to_bottom(hf)
%// place caret at the end of the texbox (=scroll to bottom)
jTxtPane = getappdata( hf , 'jTxtPane' ) ;
jTxtPane.setCaretPosition(jTxtPane.getDocument.getLength)
end
function myCloseRequestFcn(hobj,~)
isBusy = CmdWinTool('isBusy')
if isBusy==0
cleanup ; %// make sure we remove the listener
H = findall(0,'tag','figuremirror');
delete(H);
% delete(hobj) ; %// delete the figure
else
message = sprintf('System is busy to process the request.\nPlease wait.');
uiwait(msgbox(message,'Messsage'));
end
end
function cleanup
jDesktop = com.mathworks.mde.desk.MLDesktop.getInstance;
jCmdWin = jDesktop.getClient('Command Window');
jTextArea = jCmdWin.getComponent(0).getViewport.getView;
set(jTextArea,'CaretUpdateCallback',[]) ;
end
end
Now, I would like the following:
  1. Code should display the executable responses in real time. Currently, it is showing all the responses once "system" command execute the exe file completely.
  2. Ok button should only become active once system command executes completely.
  3. Close icon on the top left of the console window should not be displayed, close window will only operate using Ok button.
  4. Once command executes complete, user is allowed to close the window, and can copy the results, but not allowed to perform edits.
Please guide me how can I do it. I will be thankful to you.

Answers (2)

ES
ES on 21 Mar 2017
1. try disp
2. Set 'enable' property of the button to 'inactive' during creation and set it to 'on' after system command.
3. Not Possible as far as I know.
4. Keep txtOut enable property to inactive. Dont set it enabled.
  1 Comment
Muhammad Hammad
Muhammad Hammad on 21 Mar 2017
Thanks for your reply.
Concern 2 is resolved now. However, regarding concern 4, I already set 'txtOut' as inactive, but it doesnot allow me to copy the messages kept in the control box.
Similarly, regarding concern 1, how can I use disp command? As I actually give a path of C++ executable to "system" command. Matlab console display all the messages generated by that executable, but my Console window does not show those messages as real time.
Please let me know about it.

Sign in to comment.


Jan
Jan on 21 Mar 2017
To 3: FEX: WindowAPI can hide the Min/Max/Close button of the figure.
The approach seems to be indirect. The command window is the perfect location for the standard output, but if you want a special behavior, you can write an own output function without complicated tricks.
  3 Comments
Jan
Jan on 22 Mar 2017
Which command do you run? Note that the system command redirects the standard output to the command window already. Then redirecting it to a GUI is somewhat indirect.
Muhammad Hammad
Muhammad Hammad on 23 Mar 2017
Thanks simon, I will look into it.

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!