Skip to main content
PyCon US is happening May 14th-22nd in Pittsburgh, PA USA.  Learn more

Mlab is a high-level python to Matlab bridge that lets Matlab look like a normal python library

Project description

Mlab is a high-level python to Matlab bridge that lets Matlab look like a normal python library.
This python library is based on the work of original mlabwrap project
http://mlabwrap.sourceforge.net/
and Dani Valevski (from Dana Pe'er's lab):
http://code.google.com/p/danapeerlab/source/browse/trunk/freecell/depends/common/python/matlabpipe.py
Primer

Quick installation:

pip install mlab

Start working with the library by picking a MATLAB release that you have locally installed:

from mlab.releases import latest_release
from matlab import matlabroot

print matlabroot()

where latest_release is a MlabWrap instance, matlabroot is wrapper around MATLAB function.
Please note that matlab module is dynamically created instance, which is in this case referencing
latest_release object.

MATLAB installation discovery mechanism is implemented by mlab.releases module in such a way, that
you have to specify the release version you want to use first, by importing it. Only then you can
import from matlab module:

from mlab.releases import R2010b
from matlab import matlabroot

Also see mlab.releases.get_available_releases().

Contents

Primer

Description

Related

News

License

Download

Installation

Linux

Windows

Documentation

Tutorial

Comparison to other existing modules

What's Missing?

Implementation Notes

Troubleshooting

Strange hangs under Matlab R2008a

matlab not in path

"Can't open engine"

"`GLIBCXX_3.4.9' not found" on importing mlab (or similar)

Old Matlab version

OS X

Notes on running

Windows

Function Handles and callbacks into python

Directly manipulating variables in Matlab space

Support and Feedback

Credits
Description

Mlabwrap is a high-level python to Matlab bridge that lets Matlab look
like a normal python library.

Thanks for your terrific work on this very-useful Python tool!

George A. Blaha, Senior Systems Engineer,
Raytheon Integrated Defense Systems

mlab is a repackaging effort to make things up-to-date.


Related

Thereis is a copy of mlabwrap v1.1-pre (http://mlabwrap.sourceforge.net/) patched
as described here:
http://sourceforge.net/mailarchive/message.php?msg_id=27312822

with a patch fixing the error:

mlabraw.cpp:225: *error*: invalid conversion from const mwSize* to const int*

Also note that in Ubuntu you need to sudo apt-get install csh

For details see
http://github.com/aweinstein/mlabwrap
News

2014-08-26 1.1.3 Applying patch to add support for Windows via COM.
Credits to Sergey Matyunin, Amro@stackoverflow

2013-07-26 1.1.1 Repacking a library as mlab project. Including code
for Windows (matlabraw.cpp is off for now).

2009-10-26 1.1 fixes an incorrect declaration in mlabraw.cpp
that caused compilation problems for some users and incorporates a
setup.py fix for windows suggested by Alan Brooks. More significantly
there is a new spiffy logo!

2009-09-14 1.1-pre finally brings N-D array support, thanks to Vivek
Rathod who joined the project! Also fixed a missing import for saveVarsInMat
(thanks to Nicolas Pinto).

Since a few people have run into problems that appear to relate to compiling
Matlab C-extensions in general and aren't mlabwrap-specific, I should probably
stress that in case of any problems that look C-related, verifying whether
engdemo.c works is a great litmus test (see Troubleshooting ).

2009-03-23 1.0.1 is finally out. This is a minor release that fixes some
annoying but mostly minor bugs in mlabwrap (it also slightly improves the
indexing support for proxy-objects, but the exact semantics are still subject
to change.)

installation is now easier, in particularly LD_LIBRARY_PATH no longer
needs to be set and some quoting issues with the matlab call during
installation have been addressed.

sparse Matlab matrices are now handled correctly
(mlab.sparse([0,0,0,0]) will now return a proxy for a sparse double
matrix, rather than incorrectly treat at as plain double array and return
junk or crash).

replaced the (optional) use of the outdated netcdf package for the
unit-tests with homegrown matlab helper class.

several bugs squashed (length of mlabraw.eval'ed strings is checked, better
error-messages etc.) and some small documentation improvements and quite a
few code clean-ups.

Many thanks to Iain Murray at Toronto and Nicolas Pinto at MIT for letting
themselves be roped into helping me test my stupidly broken release
candidates.
License

mlab (and mlabwrap) is under MIT license, see LICENSE.txt. mlabraw is under a BSD-style
license, see the mlabraw.cpp.
Download

<http://github.com/ewiger/mlab>
Installation

mlab should work with python>=2.7 (downto python 2.2, with minor coaxing) and
either numpy (recommended) or Numeric (obsolete) installed and Matlab 6, 6.5,
7.x and 8.x under Linux, OS X and Windows (see OS X) on 32- or 64-bit
machines.

Linux

If you're lucky (linux, Matlab binary in PATH):

python setup.py install

(As usual, if you want to install just in your homedir add --prefix=Missing open brace for subscriptDYLD_LIBRARY_PATH$:/Applications/MATLAB701/bin/mac/
export MLABRAW_CMD_STR=/Applications/MATLAB701/bin/matlab

[Edit: I'm not sure DYLD_LIBRARY_PATH modification is still necessary.]

As far as graphics commands go, the python interpreter will need to be run
from within the X11 xterm to be able to display anything to the screen.
ie, the command for lazy people

>>> from mlabwrap import mlab; mlab.plot([1,2,3],'-o')

won't work unless python is run from an xterm, and the matlab startup
string is
changed to:

export MLABRAW_CMD_STR="/Applications/MATLAB701/bin/matlab -nodesktop"

Windows

<string>:529: (INFO/1) Duplicate implicit target name: "windows".

I'm thankfully not using windows myself, but I try to keep mlabwrap working
under windows, for which I depend on the feedback from windows users.

Since there are several popular C++ compilers under windows, you might have to
tell setup.py which one you'd like to use (unless it's VC 7).

George A. Blaha sent me a patch for Borland C++ support; search for "Borland
C++" in setup.py and follow the instructions.

Dylan T Walker writes mingw32 will also work fine, but for some reason
(distuils glitch?) the following invocation is required:

> setup.py build --compiler=mingw32
> setup.py install --skip-build

Function Handles and callbacks into python

People sometimes try to pass a python function to a matlab function (e.g.
mlab.fzero(lambda x: x**2-2, 0)) which will result in an error messages
because callbacks into python are not implemented (I'm not even it would even
be feasible). Whilst there is no general workaround, in some cases you can
just create an equivalent matlab function on the fly, e.g. do something like
this: mlab.fzero(mlab.eval('@(x) x^2-2', 0)).

Directly manipulating variables in Matlab space

In certain (rare!) certain cases it might be necessary to directly access or
set a global variable in matlab. In these cases you can use mlab._get('SOME_VAR')
and mlab._set('SOME_VAR', somevalue).
Support and Feedback

Post your questions directly on Stack overflow with tags matlab, mlab
and python


Credits

Alejandro Weinstein for patches of 1.1pre
https://github.com/aweinstein/mlabwrap

Alexander Schmolck and Vivek Rathod for mlabwrap:
http://mlabwrap.sourceforge.net/

Andrew Sterian for writing pymat without which this module would never have
existed.

Matthew Brett contributed numpy compatibility and nice setup.py improvements
(which I adapted a bit) to further reduce the need for manual user
intervention for installation.

I'm only using linux myself -- so I gratefully acknowledge the help of Windows
and OS X users to get things running smoothly under these OSes as well;
particularly those who provided patches to setup.py or mlabraw.cpp (Joris van
Zwieten, George A. Blaha and others).

Matlab is a registered trademark of The Mathworks.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page