Question

I am trying to start matlab from python, and then execute a really basic test.

def execute_matlab_command(command):
    handle = win32com.client.DispatchEx('matlab.application')
    handle.visible = True
    # By using print I know that the handle is valid here
    handle.Execute(command)

execute_matlab_handle("x=32")

The code runs fine, but matlab closes before handle.Execute(command) is called. Am I missing something here, like an open() or so? I have seen others use (almost) the same code and say that it works fine, cant wrap my head around the problem.

Edit: Matlab version r2012b, python version 2.7

Edit 2: I probably found why matlab closes. I am calling DispatchEx from a GUI built in wxPython, which contains a mainloop. I tried with a simple script without GUI and it all works fine. I am leaving this question open here if someone has the same problem or has found a solution.

Était-ce utile?

La solution

The QA Calling MATLAB functions from python indicates there should not be a problem. This Matlab page suggests you might have to configure your Matlab installation to accept being opened this way.

Note: It is not likely that the 'matlab.application', which is a COM server object, has anything to do with the matlab wrapper you found out about; the wrapper is just an executable. One of the matlab libraries has been registered as the COM server for matlab.application.

Autres conseils

use Dispatch

def execute_matlab_command(command):
    handle = win32com.client.Dispatch('matlab.application')
    handle.visible = True
    # By using print I know that the handle is valid here
    handle.Execute(command)

I haven't used DispatchEx before, but I guarantee you Dispatch will work.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top