{"id":8642,"date":"2017-06-09T09:00:03","date_gmt":"2017-06-09T13:00:03","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=8642"},"modified":"2017-06-08T01:33:04","modified_gmt":"2017-06-08T05:33:04","slug":"launch-and-manage-external-processes-from-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2017\/06\/09\/launch-and-manage-external-processes-from-matlab\/","title":{"rendered":"Launch and manage external processes from MATLAB"},"content":{"rendered":"\r\n<div class=\"content\"><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871\">Jiro<\/a>&#8216;s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/48164-process-manager\">Process Manager<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2675255\">Brian Lau<\/a>.<\/p><p>With MATLAB being able to <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/calling-external-functions.html\">interface<\/a> with other languages and environments, I pretty much do everything from within MATLAB. One operation I do quite often is controlling Microsoft&reg; applications via the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/using-com-objects-in-matlab.html\">COM interface<\/a>, such as composing and sending emails with Outlook&reg; or opening and synchronizing two PowerPoint&reg; documents.<\/p><p>But even before COM interfaces, I used to just call out to the OS to run some executables, using the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/system.html\"><tt>system<\/tt><\/a> or the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/dos.html\"><tt>dos<\/tt><\/a> command.<\/p><p>Brian&#8217;s <tt>processManager<\/tt> is the <tt>system<\/tt> command on steroids. Imagine that you want to run a program in the background, and you would like to be able to check on the status or get notified when things are done. <tt>processManager<\/tt> gives you that power.<\/p><p>As an example, I will use the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Ping_(networking_utility)\">&#8220;ping&#8221;<\/a> command that would ping a host name 15 times. This command, when run from the OS console, will test the reachability of a host multiple times and echo the response.<\/p><pre class=\"codeinput\">tic                         <span class=\"comment\">% capture the start time<\/span>\r\np = processManager(<span class=\"string\">'command'<\/span>,<span class=\"string\">'ping -n 15 www.mathworks.com'<\/span>);\r\npause(3)                    <span class=\"comment\">% pause for 3 seconds<\/span>\r\n<\/pre><pre class=\"codeoutput\">ms-www.mathworks.com [144.212.244.17]&#12395; ping &#12434;&#36865;&#20449;&#12375;&#12390;&#12356;&#12414;&#12377; 32 &#12496;&#12452;&#12488;&#12398;&#12487;&#12540;&#12479;:\r\n144.212.244.17 &#12363;&#12425;&#12398;&#24540;&#31572;: &#12496;&#12452;&#12488;&#25968; =32 &#26178;&#38291; =173ms TTL=241\r\n144.212.244.17 &#12363;&#12425;&#12398;&#24540;&#31572;: &#12496;&#12452;&#12488;&#25968; =32 &#26178;&#38291; =179ms TTL=241\r\n144.212.244.17 &#12363;&#12425;&#12398;&#24540;&#31572;: &#12496;&#12452;&#12488;&#25968; =32 &#26178;&#38291; =174ms TTL=241\r\n<\/pre><p>We can see that it&#8217;s now running. (I&#8217;m running this on a Japanese OS, hence the Japanese text)<\/p><p>Let&#8217;s just let it run in the background and turn off the display.<\/p><pre class=\"codeinput\">p.printStdout = false;      <span class=\"comment\">% turn off printing of stdout<\/span>\r\npause(3)                    <span class=\"comment\">% pause for 3 seconds<\/span>\r\n<\/pre><p>We can periodically check to see that the process is still running.<\/p><pre class=\"codeinput\">p.check                     <span class=\"comment\">% check the status of the process<\/span>\r\npause(3)                    <span class=\"comment\">% pause for 3 seconds<\/span>\r\n<\/pre><pre class=\"codeoutput\">Process  is still running.\r\n<\/pre><p>It&#8217;s been a while. Let&#8217;s see if we&#8217;re still reaching the host.<\/p><pre class=\"codeinput\">p.printStdout = true;       <span class=\"comment\">% turn on printing of stdout<\/span>\r\npause(3)                    <span class=\"comment\">% pause for 3 seconds<\/span>\r\n<\/pre><pre class=\"codeoutput\">144.212.244.17 &#12363;&#12425;&#12398;&#24540;&#31572;: &#12496;&#12452;&#12488;&#25968; =32 &#26178;&#38291; =183ms TTL=241\r\n144.212.244.17 &#12363;&#12425;&#12398;&#24540;&#31572;: &#12496;&#12452;&#12488;&#25968; =32 &#26178;&#38291; =175ms TTL=241\r\n144.212.244.17 &#12363;&#12425;&#12398;&#24540;&#31572;: &#12496;&#12452;&#12488;&#25968; =32 &#26178;&#38291; =181ms TTL=241\r\n<\/pre><p>Okay, just let me know when it&#8217;s done and tell me how long it took.<\/p><pre class=\"codeinput\">p.printStdout = false;      <span class=\"comment\">% turn off printing of stdout<\/span>\r\naddlistener(p.state,<span class=\"string\">'exit'<\/span>,@(o,e) disp([<span class=\"string\">'Done. Elapsed time: '<\/span> num2str(toc) <span class=\"string\">' seconds'<\/span>]));\r\npause(5)                    <span class=\"comment\">% pause for 5 seconds<\/span>\r\n<\/pre><pre class=\"codeoutput\">Done. Elapsed time: 14.3583 seconds\r\n<\/pre><p>There are other nice features, including the ability to<\/p><div><ul><li>create arrays of processes<\/li><li>stop a running process<\/li><li>capture all standard output and error<\/li><li>block MATLAB until the process finishes<\/li><\/ul><\/div><p>Brian also includes a few examples for you to try, and there are additional information in his GitHub page.<\/p><p><b>Comments<\/b><\/p><p>What are some of the ways you interface with external applications? Let us know <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=8642#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/48164-process-manager#comment\">comment<\/a> for Brian.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_d970555d24274399a9af6b56b6a46bc6() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='d970555d24274399a9af6b56b6a46bc6 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' d970555d24274399a9af6b56b6a46bc6';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2017 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_d970555d24274399a9af6b56b6a46bc6()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2017a<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2017a<br><\/p><\/div><!--\r\nd970555d24274399a9af6b56b6a46bc6 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871 Jiro>'s\r\n% pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/48164-process-manager Process\r\n% Manager> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2675255 Brian\r\n% Lau>.\r\n%\r\n% With MATLAB being able to\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/calling-external-functions.html\r\n% interface> with other languages and environments, I pretty much do\r\n% everything from within MATLAB. One operation I do quite often is\r\n% controlling Microsoft(R) applications via the\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/using-com-objects-in-matlab.html\r\n% COM interface>, such as composing and sending emails with Outlook(R) or\r\n% opening and synchronizing two PowerPoint(R) documents.\r\n%\r\n% But even before COM interfaces, I used to just call out to the OS to run\r\n% some executables, using the\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/system.html |system|> or the\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/dos.html |dos|> command.\r\n%\r\n% Brian's |processManager| is the |system| command on steroids. Imagine\r\n% that you want to run a program in the background, and you would like to\r\n% be able to check on the status or get notified when things are done.\r\n% |processManager| gives you that power.\r\n%\r\n% As an example, I will use the\r\n% <https:\/\/en.wikipedia.org\/wiki\/Ping_(networking_utility) \"ping\"> command\r\n% that would ping a host name 15 times. This command, when run\r\n% from the OS console, will test the reachability of a host multiple times\r\n% and echo the response.\r\n\r\ntic                         % capture the start time\r\np = processManager('command','ping -n 15 www.mathworks.com');\r\npause(3)                    % pause for 3 seconds\r\n\r\n%%\r\n% We can see that it's now running. (I'm running this on a Japanese OS,\r\n% hence the Japanese text)\r\n%\r\n% Let's just let it run in the background and turn off the display.\r\n\r\np.printStdout = false;      % turn off printing of stdout\r\npause(3)                    % pause for 3 seconds\r\n\r\n%%\r\n% We can periodically check to see that the process is still running.\r\n\r\np.check                     % check the status of the process\r\npause(3)                    % pause for 3 seconds\r\n\r\n%%\r\n% It's been a while. Let's see if we're still reaching the host.\r\n\r\np.printStdout = true;       % turn on printing of stdout\r\npause(3)                    % pause for 3 seconds\r\n\r\n%%\r\n% Okay, just let me know when it's done and tell me how long it took.\r\n\r\np.printStdout = false;      % turn off printing of stdout\r\naddlistener(p.state,'exit',@(o,e) disp(['Done. Elapsed time: ' num2str(toc) ' seconds']));\r\npause(5)                    % pause for 5 seconds\r\n\r\n%%\r\n% There are other nice features, including the ability to\r\n%\r\n% * create arrays of processes\r\n% * stop a running process\r\n% * capture all standard output and error\r\n% * block MATLAB until the process finishes\r\n%\r\n% Brian also includes a few examples for you to try, and there are\r\n% additional information in his GitHub page.\r\n%\r\n% *Comments*\r\n%\r\n% What are some of the ways you interface with external applications? Let\r\n% us know <https:\/\/blogs.mathworks.com\/pick\/?p=8642#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/48164-process-manager#comment\r\n% comment> for Brian.\r\n\r\n##### SOURCE END ##### d970555d24274399a9af6b56b6a46bc6\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\nJiro&#8216;s pick this week is Process Manager by Brian Lau.With MATLAB being able to interface with other languages and environments, I pretty much do everything from within MATLAB. One operation&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2017\/06\/09\/launch-and-manage-external-processes-from-matlab\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8642"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=8642"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8642\/revisions"}],"predecessor-version":[{"id":8646,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8642\/revisions\/8646"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=8642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=8642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=8642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}