Take the 2-minute tour ×

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.


share|improve this question
add comment

4 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.

share|improve this answer
But isn't the Matlab C library already linked in the MEX file? Or couldn't it be included in the wrapper? Of course I assume in this case that Matlab is installed on the system... –  OlivierB Jul 27 '11 at 20:26
What you're saying might be possible, but it is not what you initially asked. In that case you would need a license of Matlab and would be able to run as many sessions of the MEX file as you have licenses of Matlab, even when doing so from Python. Still it would be technically challenging to bind the Matlab C library to Python. –  carlosdc Jul 27 '11 at 22:45
Also I am not lawyer and have not read the Matlab EULA, but doing so might be of questionable legality. –  carlosdc Jul 27 '11 at 22:50
I've edited my answer a little bit. –  carlosdc Aug 3 '11 at 17:34
add comment

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.

share|improve this answer
add comment
  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 :



Returns :

mat_dict : dict

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

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

share|improve this answer
add comment

A mex function is an api that allows Matlab (i.e. 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.

share|improve this answer
add comment

Your Answer


