Launching external program from matlab
24 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Thorsten
2016-8-17
Before you load the .mat file you can wait until the file exists
while ~exist('yourmatfile.mat', 2); end
2 个评论
Walter Roberson
2016-8-22
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.
更多回答(3 个)
Dale Roach
2020-4-29
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.
0 个评论
KSSV
2016-8-17
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 个评论
Walter Roberson
2016-8-17
... 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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Use COM Objects in MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!