Call multiple instances of another program from MATLAB?

I'm writing a program that generates files to be simulated in a third party program. I would like to be able to call multiple instances of this third party program from within my MATLAB GUI.
I am able to call a single instance of the program using the "system" command, but MATLAB becomes busy until the simulation in the other program is complete. Sometimes the simulation can take several hours. I would like the ability to split up my input files into several smaller files and call multiple instances of this third party program from a single MATLAB program. Is it possible to call the "system" command without MATLAB becoming busy?
Is the Parallel Computing Toolbox required to do this?
Thank you, Miles

2 Comments

Will you be waiting for these multiple 3rd party programs to finish before your MATLAB code continues? Or do you just want to spawn these programs off to run and finish on their own without MATLAB needing to check on their progress? How many instances will you simultaneously run?
I do not need to wait for the 3rd party programs to finish. I just want to spawn them. Right now I'm thinking of running 5-10 instances simultaneously.

Sign in to comment.

 Accepted Answer

If you add the character '&' to the end of your system() command line, then system() will return as soon as the given program starts, without waiting for any output.
Depending on what you are doing, sometimes that is all you need -- to launch the commands and let them run.
Sometimes, though, you need to deal with the output of the program. If your system() command can tell the external program which output file to write into, sometimes you can check back to see if the file is ready. Doing that properly can be trickier than it sounds at first: you will not necessarily be able to just check if the output file exists, because the program might start writing to the file and might have it open for some time. There is no signalling mechanism for programs to say "Okay, I am finished with this file"; sometimes you can work something out with operating-system-specific "lock" facilities. Sometimes you can tell the readiness by the file size. Sometimes you might be willing to assume it is ready if it has not changed size in "sufficiently long". Sometimes you have to resort to checking to see which processes exist so you can tell whether the process exited.
If you are using MS Windows then there are ActiveX controls to create processes that you could use instead of system().
The Parallel Computing Toolbox has the benefit that you would not have to change anything about handling the external process other than making sure that any output files (and temporary files) have unique names so the copies do not clash with each other: you would system() up a particular invocation without using '&' and that worker would wait for that process to end.

More Answers (0)

Categories

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