Matlab returns exit code immediately

2019-06-25 23:39发布

问题:

I am creating various matlab .m-files with python and then run them using subprocess. When the files are finished I would like to delete them:

    command = ['C:\\MatlabR2012b\\bin\\matlab.exe', '-nodesktop', '-nosplash', '-r', 'mfile']
    matlab = subprocess.Popen(command) # launch matlab with m file
    matlab.wait() # wait for matlab to finish before deleting .m file
    print "delete"
    os.remove(self.filename)

The problem is that matlab.wait() never waits, since matlab returns exit code 0 immediately. Is there another way to check if matlab has finished?

回答1:

On Windows, there is both bin\matlab.exe and bin\win32\matlab.exe (or bin\win64\matlab.exe). The former is just a wrapper around the latter and pretty much exits immediately.

You can either call bin\win32\matlab.exe directly or use the -wait option when calling bin\matlab.exe.