Running an external program from Matlab
7 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(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. Also, you would need to modify that contribution to use it with MS Windows.
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.
You appear to be using MS Windows and you do not appear to using ActiveX. You appear to be passing a command line argument that is a file of commands. Unless Dymola has a command option similar to "execute this and then listen on standard input for more commands", your use of Dymola is not compatible with any of the available technologies.
What you should probably be doing is looping around creating the contents of the scripts, but not handing the scripts off to Dymola within the loop. Instead, built all the scripts and concatenate them all together into one big script, and then ask Dymola to execute that.
0 个评论
per isakson
2016-8-22
编辑:per isakson
2016-8-22
Two different alternatives
- use the DOS command, tasklist, to find out whether a Dymola process already exists.
- use System.Diagnostics.Process to both start Dymola and keep track of it's process.
I prefer System.Diagnostics.Process to handled external programs because it provides better control. It provides useful information on how the external process is running and it's kill method might be useful in case Dymola hangs.
2 个评论
per isakson
2016-8-24
编辑:per isakson
2016-8-24
You have to use something like
if not( dymola_process exists )
start dymola_process
end
"every iteration, the contents of this script is changed"   See http://uk.mathworks.com/matlabcentral/answers/132385-co-simulation-between-matlab-and-dymola#answer_141514
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!