External program run/kill from matlab

16 views (last 30 days)
Mark Pleasants
Mark Pleasants on 23 Aug 2016
Commented: Sajid Afaque on 12 Dec 2019
Hi all,
I am trying to run an external program from matlab as part of an optimization problem. My issue is that sometimes my external program will run into numerical issues and hang, so that the optimization will run forever and not finish. Currently I am using the following code to set a time limit on my program such that if it runs longer than a certain time, I know that it will not finish so I kill it and the optimization keeps going.
!path_to_my_prg/myprg.exe &
pause(n) % where n is a certain number of seconds that I know is too long for the program to still be running
!taskkill -f -im myprg.exe
So if the program runs longer than n seconds, it stops. The only issue I have is that it pops up a new window every time I call my program and makes it very difficult to keep working on anything else on my computer. My question is if there is any way to have the matlab script keep running while also having my external program running within matlab. I know I can remove the '&' and it won't pop up a new window, but then I have to wait for the program to finish before the script can continue, so if my program hangs, I need to kill the entire optimization.
I have also tried using
system('start /b path_to_my_prg\myprg.exe');
to have it run completely in the background but have run into other issues since I cannot keep track of what the program is doing at all. So if there is another issue I won't know about it until the optimization ends in a number of hours or I kill it.
I do not have access to the source code for my .exe so I cannot make any changes within the program itself.
So is it possible to to have the matlab script keep running as with '&' but keep the program running within the command window as without '&'?
  2 Comments
KAE
KAE on 11 Oct 2017
For other readers unfamiliar with using '&' in the external command to run in the background, here is some info.
Sajid Afaque
Sajid Afaque on 12 Dec 2019
Hey everyone,
even i am using the same stratergy to call an external function and pause for several minutes then kill the process.
it is fine when my external process get into some infinite numerical calculations or it gets strucks.
but my problem is it waits for "n" amount of time even for process that finishes early that n.
can any1 suggest any approach where it waits and kills only when the process gets struck.
it should not wait for the good ones.

Sign in to comment.

Answers (1)

Mark Pleasants
Mark Pleasants on 23 Aug 2016
I think I may have found an answer to my own question... this code seems to be doing the trick
system(start /min path_to_my_prg/myprg.exe');
the /min brings up the new window minimized so I can check it but it doesn't interrupt any other work I am doing
found the answer here

Categories

Find more on Programming 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!