Running an external program from Matlab

16 visualizzazioni (ultimi 30 giorni)
partha das
partha das il 22 Ago 2016
Modificato: per isakson il 24 Ago 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?

Risposte (2)

Walter Roberson
Walter Roberson il 22 Ago 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. 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.

per isakson
per isakson il 22 Ago 2016
Modificato: per isakson il 22 Ago 2016
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 Commenti
partha das
partha das il 22 Ago 2016
Modificato: per isakson il 24 Ago 2016
I am using the following code -
proc = System.Diagnostics.Process();
proc.StartInfo.FileName = fullfile('C:\\Program Files\\Dymola 2016 FD01','bin','Dymola');
proc.StartInfo.Arguments = 'C:/Users/PSD/Documents/Dymola/pend.mos' ;
proc.Start();
'pend.mos' is a Dymola script which i am generating from Malab (by performing the file write operations). In every iteration, the contents of this script is changed. If i use the above code, then in every iteration Dymola window will open and the script will run. So at the end of all iterations, i will get a lot of Dymola windows, which i don't want.
I want that Dymola should be opened only once. In that opened Dymola window, i want to run the modified 'pend.mos' script in every iteration. Is there a way to pass arguments to an opened process?
per isakson
per isakson il 24 Ago 2016
Modificato: per isakson il 24 Ago 2016
You have to use something like
if not( dymola_process exists )
start dymola_process
end

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by