Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exit after running matlab script from command line?

Tags:

python

matlab

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:

enter image description here

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!

like image 431
laike9m Avatar asked Dec 02 '25 10:12

laike9m


1 Answers

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'])
like image 60
David Heffernan Avatar answered Dec 04 '25 22:12

David Heffernan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!