batch file - matlab - display dos command output to static text -


i using gui call terminal command. using dos(my_command, '-echo') can command output in matlab's command window. there anyway replicate -echo in static text in gui?

currently, my_command, write output log file, , update static text's string log file after process finishes, want live-view in command window: output displayed line line in real-time. thanks.

update:

@hoki: question is: when console commands being executed, if close gui matlab returns errors unstopable, how can freeze whole gui until commands finished?

enter image description here

thanks yair altman info in article, got work involves hacking matlab java base objects (namely command window).

commandwindowgui

it involves attaching listener matlab command window. careful, save work , prepared kill matlab process quite few times until right ... because every time have error in code stuck in kind of infinite loop (the error/warning sent command window, triggers listener, re-trigger error etc ...). had restart matlab dozen of times example below work in stable way.

that's reason why attach listener temporarily. before sending dos command , remove listener directly after. can leave listener permanently or adjust needs remember advice above. consider command window can hold massive amount of character, may not want in textbox, have manage text (take subset in example), , consider if want append or refresh text in textbox.

the example below seems stable is, modification @ own risks ;-)

after request in comment added 3 functions:

  • an oncleanup. matlab functionality allow last resort action in case goes wrong (a kind of "catch all" mechanism). heavily recommended kind of program using undocumented functions.

  • a mycloserequestfcn intercept closing action of window remove listener , avoid error loops.

  • a scroll_to_bottom function. 1 allows move text box caret end of text (= scroll bottom in case there more text visible space).

warning: last functionality deserve separate question , again call undocumented functionality (so compatibility never guaranteed). able implement need have findjobj function available in matlab path. if not want download external component, delete/comment part of code uses , subfunction scroll_to_bottom (and forget scrolling text box, there no way in pure matlab). or can pick previous version of code looking @ edit history of post.


function h = gui_mirrorcmdwindow  %% // remove listener in case goes wrong closeup = oncleanup(@() cleanup);  %% // create figure , uicontrol h.f = figure; h.txtout = uicontrol(h.f,'style','edit','max',30,'min',0,...                    'horizontalalignment','left',...                    'fontname','fixedwidth',...                    'units','normalized',...                    'enable','on',...                    'position',[.05 .2 .9 .75]);  h.btnping = uicontrol(h.f,'style','pushbutton',...                    'string','ping',...                    'units','normalized',...                    'position',[.05 .05 .9 .1],...                    'callback',@btnping_callback);  guidata(h.f,h)  %// intercept close request function cleanup before close set(gcf,'closerequestfcn',@mycloserequestfcn)   %% // handle of matlab control window jdesktop = com.mathworks.mde.desk.mldesktop.getinstance; jcmdwin = jdesktop.getclient('command window'); jtextarea = jcmdwin.getcomponent(0).getviewport.getview;  %% // handle of 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 ) ;   function btnping_callback(hobj,~)     h = guidata(hobj) ;     jtextarea = getappdata( h.f , 'jtextarea' ) ;      my_command = 'ping google.com -n 10' ;     startpos = jtextarea.getcaretposition ;     set(jtextarea,'caretupdatecallback',{@commandwindowmirror,h.f,startpos}) ;     dos( my_command , '-echo' ) ;     pause(1) %// make sure catch last echo before kill callback     set(jtextarea,'caretupdatecallback',[]) ;     scroll_to_bottom(h.f)   function commandwindowmirror(~,~,hf,startpos)     h = guidata(hf) ;     jtextarea = getappdata( h.f , 'jtextarea' ) ;      %// retrieve text since start position     txtlength = jtextarea.getcaretposition-startpos ;     if txtlength > 0 %// in case smart bugger pulled 'clc' between calls         cwtext = char(jtextarea.gettext(startpos-1,txtlength) ) ;      end     %// display in gui textbox     set( h.txtout, 'string',cwtext ) ;      scroll_to_bottom(h.f)   function scroll_to_bottom(hf)     %// place caret @ end of texbox (=scroll bottom)     jtxtpane  = getappdata( hf , 'jtxtpane' ) ;     jtxtpane.setcaretposition(jtxtpane.getdocument.getlength)   function mycloserequestfcn(hobj,~)     cleanup ;       %// make sure remove listener     delete(hobj) ;  %// delete figure   function cleanup     jdesktop = com.mathworks.mde.desk.mldesktop.getinstance;     jcmdwin = jdesktop.getclient('command window');     jtextarea = jcmdwin.getcomponent(0).getviewport.getview;     set(jtextarea,'caretupdatecallback',[]) ; 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -