Hello,
First of all, I'm sorry if my question seems silly, but I am very unfamiliar with Python.
I am using a particular python code base which, in a Linux system, solves the problem I am working on (I am using Windows). A major part of the python code's task is to generate and run certain matlab scripts. The code starts matlab subprocesses by an os.system(command). Under Mac/Linux, it seems that python code waits until the matlab script terminates; however, under windows, the code simply proceeds before the matlab script terminates which crashes the application. I am wondering how I could rewrite the code to ensure that the python script waits for matlab subprocess to terminate.
The function I am using is below:
def runMatlab(self,dirname,scriptname):
""" run matlab as a unix command
commands: the list of command lines for matlab
Returns the output from the matlab commands
"""
logfile = scriptname+".log"
scriptname = scriptname.rstrip(".m")
scriptname = os.path.basename(scriptname)
cwd=os.getcwd()
os.chdir(dirname) # matlab is not happy with running a script not in the current directory
command=self.runInfo.matlab+' -logfile '+logfile+' -nojvm -nosplash -nodisplay -r '+scriptname
print "matlab command=|"+command+"|\n"
print "current directory is:"+os.getcwd()+"\n"
self.matlabStatus = os.system(command)
os.chdir(cwd) # switch back to original working directory
if self.matlabStatus != 0:
print "Matlab terminated with error: check logfile "+ dirname+logfile
sys.exit(1)
I tried to modify os.system(command) as
filename=self.runInfo.matlab
argin=(' -logfile ',logfile,' -nojvm',' -nosplash',' -nodisplay', '-r', scriptname)
os.spawnv(os.P_WAIT,filename,(filename,)+argin)
but I guess this is invalid syntax...
Any help would be greatly appreciated,
Thank you very much in advance,
Ilya