How can I open one instance of Matlab using win32com?

I am using Matlab COM interface from python like this:

import win32com.client
mlab = win32com.client.Dispatch('matlab.application')
mlab.visible = True
print(mlab.Execute("1 + 1"))

      

When I run the above code, it prints correctly ans = 2

, but Matlab (command window) exits after executing the code. I only want to open one instance of Matlab and send commands to it, not start a new Matlab session every time. Is there a way to achieve this? For the record, I am using Python 3.4.3 and Matlab R2014A.

+3


source to share


1 answer


This is the destructor mlab

that comes out of Matlab. Save this variable and your Matlab session will remain open.



I recommend only submitting once and using this com instance wherever you need it, that way the garbage collector will do exactly what you want, close matlab as soon as you don't use it anymore.

+2


source







All Articles