File Exchange Pick of the Week

Our best user submissions

Launch and manage external processes from MATLAB

Jiro‘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 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.

But even before COM interfaces, I used to just call out to the OS to run some executables, using the system or the dos command.

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 [144.212.244.17]に ping を送信しています 32 バイトのデータ:
144.212.244.17 からの応答: バイト数 =32 時間 =173ms TTL=241
144.212.244.17 からの応答: バイト数 =32 時間 =179ms TTL=241
144.212.244.17 からの応答: バイト数 =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
144.212.244.17 からの応答: バイト数 =32 時間 =183ms TTL=241
144.212.244.17 からの応答: バイト数 =32 時間 =175ms TTL=241
144.212.244.17 からの応答: バイト数 =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.

Comments

What are some of the ways you interface with external applications? Let us know here or leave a comment for Brian.




Published with MATLAB® R2017a

|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.