Insert function from Matlab MEX file directly in Python

I use my own Matlab MEX file to import some simulation results into Matlab (of course, no source code exists!). The interface with Matlab is actually very simple, as there is only one function that returns a Matlab structure. I would like to know if there is a way to call this function in a MEX file directly from Python without using Matlab?

What do I mean, for example, using something like SWIG to import a C function in Python, by providing your own Matlab wrapper around it ... By the way, I know that you can already read data files with scipy.io.loadmat binary * .mat Matlab file, but I don’t know if the data representation in the mat file is the same as the internal representation in Matlab (in this case it may be useful for the MEX shell).

The idea, of course, would be able to use the function provided in MEX without installing Matlab present in the system.

Thanks.

+7
source share
4 answers

If I don’t understand something about how Matlab works or about your question, this is very unlikely. From a technical point of view, any solution should be a complete, binary, compatible, error for error, the possibility of re-implementing the Matlab C library (implementation of mxGetPr, mxGetN, etc.), but binding to Python.

Let me edit my own answer to say the following: if you have a MATLAB license, there is a great MLAB wrap package that does at least part of what you want.

+2
source

You can create stand-alone shared libraries from Matlab code, for example http://www.mathworks.com/help/toolbox/compiler/mbuild.html . This way you should be able to call from python. But you need the Matlab compiler, however, it looks like the trial version is available for free.

See also the section https://stackoverflow.com/a/3186269/2128 .

+2
source
  • You can create a library from the mex file as Mauro pointed out
  • You can safely use scipy.io.loadmat, a data view:

from

http://docs.scipy.org/doc/scipy/reference/generated/scipy.io.loadmat.html

Returns:

mat_dict: dict

with variable names as keys and loaded matrices as values

The loaded matrices are saved the way you saved them, i.e. data presentation should be coherent.

+1
source

The mex function is an api that allows Matlab (i.e. the Matlab program) to call a function written in c / C ++. This function, in turn, can call Matlab's own functions. Thus, the mex function will be associated with the Matlab libraries. Thus, calling the mex function directly from a Python program without the Matlab libraries is not possible (and that makes no sense).

For reasons why the mex function was created in the first place? Should it be done for some non-matlab c libraries (or c-code) for Matlab users, or would it have to hide some proprietery matlab code code while still making it available to Matlab users? If this is the first case, then you can ask the owners of the mex function to provide it in a non-micro-dynamic form of lib, which you can include in another c or python program. This should be easy if the mex function is independent of Matlab's internal functions.

Others mentioned the matlab compiler ... yes, you can enable the mex function in standalone binary code that can be called from unix (thus, from python, but as a unix call) if you use the Matlab Compiler to create such a binary. This will require deploying the binary with the Matlab runtime. This is not quite the same as calling a function directly from python - for example, there are no return values.

+1
source

All Articles