Sports Biomechanics Lab > Blog > 2012 > February > 19 > Python - Matlab integration
Personal tools

Python - Matlab integration

by Jason Moore — last modified Feb 19, 2012 03:20 PM
Filed Under:

There are some tools in Matlab that don't exist in Python. How can I seamlessly make use of Matlab tools with Python?

I've been making use of Matlab's System Identification toolbox extensively, as there really isn't anything on the open source market that does the same kind of thing. I could write my own stuff in Python, but I don't understand it all well enough yet to do that, not mention not really having the time. I'd really like to be able to do all my data pre- and post-processing with Python, as most of my tools are already in the language and it's so much more pleasurable to work with. I've been calling Matlab scripts with Python that write data to the filesystem and then I retrieve it and parse it in Python, but this is super clunky. Turns out there are some tools to integrate Python and Matlab better. The best looking one for my situation seems like it will be mlabwrapper, which I will try out soon.

pymat and pymat2
A basic library for python that allows you to open a Matlab session, send and retrieve variables from the Matlab workspace, and evaluate Matlab commands. It only works for the simplest data types. The module is old and not active but someone forked it (pymat2) and has it working for later versions of Matlab and Python. pymat2 also hasn't seen any development in the last couple of years.
This takes the pymat idea to a higher level. You can do the same thing as pymat (but with error reporting), but you can also call Matlab commands directly on your python data types (not putting and getting required). It has error reporting and even can handle the more abstract data types (some are come in as proxy objects). This seems to be still actively developed and pretty promising.
This code actually tries to rewrite your Matlab code into equivalent Python code. I assume it has limitations because there isn't necessarily an equivalent Python library for every Matlab one, but it probably handles the basics.
This software is recently developed and allows you to do what pymat does, except it is a two way street. You can run Python commands in Matlab and Matlab commands in Python. 
The latest Matlab .mat files are actually in the HDF5 format. These can potentially be read with a Python HDF5 library such as h5py or PyTables. Matlab also provides HDF5 read write capabilities, so the file could be used as a bridge. Any filesystem file could be used this way, with a file type that is easily readable/writeable by both Python and Matlab being the preferred one. But this method is potentially slow and not really ideal. A colleague of mine, Adrian Cooke, is developing an interface to PyTables HDF5 from Python and Matlab for which load data from a variety of data sources:, which looks promising.
Document Actions

IPython notebook can call Matlab

Avatar Posted by Jason Moore at Nov 14, 2012 04:24 PM
I just found this: which gives matlab cell magics for the IPython notebook:[…]/matlab_magic.ipynb

Add comment

You can add a comment by filling out the form below. Plain text formatting.

Please enter your name.
Enter the word