2

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.

Pphoenix
  • 1,423
  • 1
  • 15
  • 37

2 Answers2

1

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.

Community
  • 1
  • 1
Oliver
  • 27,510
  • 9
  • 72
  • 103
0

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.

laike9m
  • 18,344
  • 20
  • 107
  • 140
  • Matlab starts but shuts down when it is fully loaded, without running the command. Dispatch dont autoclose? – Pphoenix Apr 16 '14 at 13:45