Clear Filters
Clear Filters

Launching external program from matlab

35 views (last 30 days)
I am launching an external program from my Matlab code (on windows) using "system" command. My issue is that when i launch it this way, matlab will wait for the external program to close before it continues, which i don't want. Any solution to this?

Accepted Answer

Thorsten
Thorsten on 17 Aug 2016
Before you load the .mat file you can wait until the file exists
while ~exist('yourmatfile.mat', 2); end
  2 Comments
partha das
partha das on 22 Aug 2016
I am using "system" command in an iterative Matlab algorithm to launch Dymola. But "system" command opens the external program (Dymola in my case)every time(once in every iteration) it is called. I am not exiting the Dymola window. Instead i am using "&" to continue the matlab execution. As result i am getting a lot of opened Dymola windows.
I want that only in the first iteration, Dymola window is opened using "system". But from 2nd iteration onwards, "system" should not open another Dymola window. Is there a way to do this?
Walter Roberson
Walter Roberson on 22 Aug 2016
In order for that to work properly, you would need some way of telling the Dymola window what commands you want to execute at the start, and you would need some way of sending it new commands. You would also need some way of finding it what it is doing, because you would not be able to "queue" commands for it .
In order to be able to send another process commands dynamically, you need to use one of the following technologies:
1) process open, unix popen(), MS Windows _popen() . This is not designed for the case where commands must be given by pressing keys or clicking on buttons: this is for the case where commands can be given by an input stream of bytes and results retrieved by an output stream of bytes. There is a File Exchange contribution for doing popen(), but I/O can only happen in one direction for it, but your case probably needs I/O in both directions
2) TCP (or UDP). This requires that the program be designed to accept connections and receive commands and send results. It has the advantage of being fully supported in the Instrument Control toolbox and as well there is a File Exchange contribution "tcpudpip". This is more general than popen() but requires specific programming to support, and that probably does not exist in your case.
3) ActiveX / COM. This requires that the program be designed for it, and is effectively only an option for MS Windows.
4) Java Robot class. This is the most flexible in terms of sending keystrokes and mouse clicks, but it can get difficult to retrieve information.

Sign in to comment.

More Answers (3)

Walter Roberson
Walter Roberson on 17 Aug 2016
Add "&" to the end of the command line.

Dale Roach
Dale Roach on 29 Apr 2020
If you want to launch an application and don't want MATLAB to wait for it to close before returning control to your script, use the "start" command. Here's an example:
system('start notepad.exe testfile.txt');
Here I've launched notepad and told it to open the file testfile.txt. When you use the "start" command, it returns control to the system right away, so your scirpt can continue to run.

KSSV
KSSV on 17 Aug 2016
I guess you are running the program via command prompt...why dont you exit the program at command prompt itself, so that MATLAB can come out of system?
  4 Comments
partha das
partha das on 17 Aug 2016
Edited: partha das on 17 Aug 2016
I tried with "&". But in my algorithm the matlab codes immediately after the "system" command require the .mat file generated by the external program DYMOLA. With "&" , matlab will continue execution of the code after "system" command with the external program executing in background. The generation of .mat file by DYMOLA takes some time.So i get an error in Matlab that .mat file is missing.
Is there a way to suspend the matlab execution untill the .mat file is generated by the external program?
Walter Roberson
Walter Roberson on 17 Aug 2016
... remove the & so that system() waits for the program to end?
If you cannot continue the MATLAB program until the .mat file is generated, then the only advantage of not using & is that you would be able to implement a Quit / Cancel button for impatient users.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!