bastibe / transplant

Transplant is an easy way of calling Matlab from Python
https://transplant.readthedocs.io
Other
110 stars 26 forks source link

connect to existing matlab session #70

Closed bsxfun closed 5 years ago

bsxfun commented 5 years ago

I know that closing Matlab from iPython-launched transplant connection is currently an open problem, so I'm wondering if there's a recommended way to connect to an open transplant-Matlab-connection?

that means, effectively, you could run a script twice using the same connection. the first time it would open, and the second time it would re-use the same connection. I guess that means you have to use '%run -i' to start with...

bastibe commented 5 years ago

You could re-use an existing global variable that contains your Matlab instance. As long as this variable doesn't go out of scope or is explicitly closed, it will not terminate.

Actually, closing Matlab is no problem as far as I am aware of, so long as you close it explicitly. The only issue is that IPython can prevent variables from going out of scope, and therefore from being garbage collected (which would implicitly close the connection). But Matlab.close() should always work.

bsxfun commented 5 years ago

ok thanks... for the record my implemented solution is:

try:
    eng.disp("existing connection")
except:
    eng = transplant.Matlab()
    eng.disp("new connection")

and I'm just running with '%run -i', but I didn't need a global variable. If you have a more recommended implementation/solution let me know.

bastibe commented 5 years ago

Cool, so this solved your problem?

bsxfun commented 5 years ago

my hack solved the problem, but not sure it's optimal... anyway, i'll close this issue..

thanks for the help and great work!