Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed a function from a Matlab MEX file directly in Python

Tags:

python

matlab

mex

I am using a proprietary Matlab MEX file to import some simulation results in Matlab (no source code available of course!). The interface with Matlab is actually really simple, as there is a single function, returning a Matlab struct. I would like to know if there is any way to call this function in the MEX file directly from Python, without having to use Matlab?

What I have in mind is for example using something like SWIG to import the C function into Python by providing a custom Matlab-wrapper around it... By the way, I know that with scipy.io.loadmat it is already possible to read Matlab binary *.mat data files, but I don't know if the data representation in a mat file is the same as the internal representation in Matlab (in which case it might be useful for the MEX wrapper).

The idea would be of course to be able to use the function provided in the MEX with no Matlab installation present on the system.

Thanks.

like image 814
OlivierB Avatar asked Jul 27 '11 17:07

OlivierB


2 Answers

Unless I'm misunderstanding something about how Matlab works or about your question, it is very highly unlikely to be possible. From a technical point of view any solution would need to be a full, binary compatible, bug for bug, feature for feature reimplementation of the Matlab C library, (implementing mxGetPr, mxGetN and so on) but binding to Python.

Let me edit my own answer to say the following: If you do have a MATLAB license available there is the excellent package MLAB wrap which does at least part of what you want.

like image 124
carlosdc Avatar answered Sep 28 '22 23:09

carlosdc


You can create stand alone shared libraries from Matlab code, e.g http://www.mathworks.com/help/toolbox/compiler/mbuild.html. These you should be able to call from python. But you need the Matlab Compiler, however, it looks like there is a trial version of it available for free.

See also this stackoverflow topic.

like image 27
Mauro Avatar answered Sep 29 '22 00:09

Mauro