Reputation: 19388
Here is my python code
DosCmd = 'matlab -wait -automation -nosplash -r "run \'' + to_run + "'\""
os.system(DosCmd)
curve_file = open('curve/'+str(index)+'.curve','r')
I run a .m file in a python script,it works fine but after executing the .m file,it is stuck in os.system(DosCmd)
.
To make python run the following code,I have to close this window:
Since this part of code is in a loop,it really disturbs me. I found someone on the Internet says that matlab can exits automatically after executing the .m file,but mine just doesn't.Will someone tell what I did wrong or what should I do?Thx!
Upvotes: 3
Views: 3744
Reputation: 358
Add the command exit
to the last line of your script.
The -wait commandline switch means the starter application won't close until matlab exits. If you are acutally having python do something with the ML output, then -wait is correct, otherwise get rid of the -wait.
Also, are you sure you really want to be launching new matlab session each time in a loop? Matlab exposes DDE functionality, which would allow you to open one instance and send commands.
Or, you might look at PyMat, or mlabwrap, etc, one of the existing python to matlab bridge libraries.
Upvotes: 2
Reputation: 613481
Add a call to exit
to the MATLAB code that you execute.
DosCmd = 'matlab -wait -automation -nosplash -r "run \'' + to_run + "', exit\""
Your quoting looks a little wonky mind you, but you just need to add , exit
to the end of the command that you pass in the -r
argument.
By the way, this would be a lot easier with subprocess
so that you could let subprocess
do the quoting for you.
subprocess.check_call(['matlab', '-wait', '-automation', '-nosplash',
'-r', 'run \' + to_run + \', exit'])
Upvotes: 5