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!
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'])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With