简体   繁体   中英

Embed a function from a Matlab MEX file directly in Python

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.

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.

You can create stand alone shared libraries from Matlab code, eg 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 .

A mex function is an api that allows Matlab (ie a matlab program) to call a function written in c/c++. This function, in turn, can call Matlab own internal functions. As such, the mex function will be linked against Matlab libraries. Thus, to call a mex function directly from a Python program w/o Matlab libraries doesn't look possible (and doesn't makes sense for that matter).

Of consideration is why was the mex function created in the first place? Was it to make some non-matlab c libraries (or c code) available to matlab users, or was it to hide some proprietery matlab-code while still making it available to matlab users? If its the first case, then you could request the owners of the mex function to provide it in a non-mex dynamic lib form that you can include in another c or python program. This should be easy if the mex function doesnt depend on Matlab internal functions.

Others above have mentioned the matlab compiler... yes, you can include a mex function in a stand alone binary callable from unix (thus from python but as a unix call) if you use the Matlab Compiler to produce such binary. This would require the binary to be deployed along with Matlab's runtime environment. This is not quite the same as calling a function directly from python-- there are no return values for example.

  1. You can build a library from a mex file as Mauro pointed out
  2. You can use scipy.io.loadmat safely, the data representation :

from:

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

Returns :

mat_dict : dict

dictionary with variable names as keys, and loaded matrices as values

The loaded matrices are as you saved them, ie data representation should be coherent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM