With MATLAB being able to interface with other languages and environments, I pretty much do everything from within MATLAB. One operation I do quite often is controlling Microsoft® applications via the COM interface, such as composing and sending emails with Outlook® or opening and synchronizing two PowerPoint® documents.
Brian’s processManager is the system 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. processManager gives you that power.
As an example, I will use the “ping” 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.
tic % capture the start time p = processManager('command','ping -n 15 www.mathworks.com'); pause(3) % pause for 3 seconds
ms-www.mathworks.com [22.214.171.124]に ping を送信しています 32 バイトのデータ: 126.96.36.199 からの応答: バイト数 =32 時間 =173ms TTL=241 188.8.131.52 からの応答: バイト数 =32 時間 =179ms TTL=241 184.108.40.206 からの応答: バイト数 =32 時間 =174ms TTL=241
We can see that it’s now running. (I’m running this on a Japanese OS, hence the Japanese text)
Let’s just let it run in the background and turn off the display.
p.printStdout = false; % turn off printing of stdout pause(3) % pause for 3 seconds
We can periodically check to see that the process is still running.
p.check % check the status of the process pause(3) % pause for 3 seconds
Process is still running.
It’s been a while. Let’s see if we’re still reaching the host.
p.printStdout = true; % turn on printing of stdout pause(3) % pause for 3 seconds
220.127.116.11 からの応答: バイト数 =32 時間 =183ms TTL=241 18.104.22.168 からの応答: バイト数 =32 時間 =175ms TTL=241 22.214.171.124 からの応答: バイト数 =32 時間 =181ms TTL=241
Okay, just let me know when it’s done and tell me how long it took.
p.printStdout = false; % turn off printing of stdout addlistener(p.state,'exit',@(o,e) disp(['Done. Elapsed time: ' num2str(toc) ' seconds'])); pause(5) % pause for 5 seconds
Done. Elapsed time: 14.3583 seconds
There are other nice features, including the ability to
- create arrays of processes
- stop a running process
- capture all standard output and error
- block MATLAB until the process finishes
Brian also includes a few examples for you to try, and there are additional information in his GitHub page.
Get the MATLAB code
Published with MATLAB® R2017a
Comments are closed.
1 CommentsOldest to Newest
Creating an external process from within your Mex-file is not hard. I often create a pair of anonymous pipes and attach standard in- and output of the process to these. Using shared moemry requires a bit more care.