4

I am trying to use Matlab functions in python and would like to have something similar to the following usage:

import matlab_wrapper
matlab = matlab_wrapper.MatlabSession()
f = lambda x: (1.0*x - 0.5)**2.0
matlab.workspace.fminsearch(f, 1.0)

There are a few Matlab wrappers for Python out there, but I need Matlab to evaluate a Python function which is defined in the console. If this is even possible I would like to extend this to non standard matlab functions such as:

matlab_wrapper.call.('some_matlab_function', python_function, numpy_array)

The alternative is to try to build a wrapper to call my python functions in matlab, bu that would require me writing and compiling python files and executables, and working in two different languages, which I would like to avoid.

oliversm
  • 1,771
  • 4
  • 22
  • 44
  • 1
    matlab can't evaluate python bytecode. I'm not well versed enough with matlab itself to know if it can evaluate compiled binaries as first class functions (what would be the case if you compiled `f`). If you can, I would suggest the ahead of time compilation options from `numba`. As a python snob, I would tend to suggest you start to move away from matlab, as most everything you can do in it is also possible with `numpy`, `scipy`, and `matplotlib`. [Here's](http://stackoverflow.com/q/19070943/3220135) the scipy equivelent of fminsearch – Aaron May 17 '17 at 17:32
  • Upon further inspection [matlab can load dll's](http://www.mathworks.com/help/matlab/ref/loadlibrary.html), but I'll leave you to figure out how to get it to dynamically compile then provide the library path to the matlab shell. It's just a bit out of my comfortable zone of expertise. – Aaron May 17 '17 at 17:40
  • 1
    @Aaron, I loath matlab, but I want to use TOMLAB in a study I am doing, and it is only available in Matlab. Otherwise I never use Matlab. – oliversm May 17 '17 at 17:47
  • I would suggest It may be easier (from a framework standpoint) to take the individual solvers (looks like they're mostly fortran) and wrap those in python to skip the matlab element. numpy has native support for fortran ordered arrays, which could make this easier (obvi, with some ctypes tomfoolery) – Aaron May 17 '17 at 18:18
  • looking into the issue a bit.. It looks like matlab requires a header file along with the shared library, and I'm not sure numba necessarily generates one on compilation. It looks like either a *.pyd* or *.so* file is generated (OS dependent), but nothing is mentioned about the *.h* file – Aaron May 17 '17 at 18:34

0 Answers0