Skip to main content

A pythonic interface to MATLAB

Project description

Python interface to MATLAB (pymatlab)

This package lets Python users interface and communicate with MATLAB from Python. Pymatlab makes it easier for users to integrate a project with a large MATLAB codebase into python scripts by using MATLAB scripts as a part of the python program.

The basic functionality of this package is to send data from Python to MATLAB’s workspace to be able to run Matlab function on the data. After processing you retrieve back data to python. This enables you to process data with Mathlab’s built in functions, toolboxes or Matlab-scripts. It is also possible to use MATLAB’s to generate plots or other graphics.

The package uses Numpy’s ndarrays and translates them into MATLAB’s mxarrays using Python’s ctypes and Matlab’s mx library. The interface to MATLAB’s workspace in done through MATLAB’s engine library.


Downloading is possible from PyPi and SourceForge pymatlab files. Since pymatlab is hosted at SourceForge the latest development version is available from git. There are different branches available this is the ctypes variant.


Standard installation method using pip, easy_install or ‘python install’.

Preparing to use pymatlab

You need MATLAB from Mathworks properly installed on your local machine.


C-shell has to be installed in order to make the Matlab connection work. Also the path to the matlab binary needs to be set.

$ sudo apt-get install csh

$ export PATH = /opt/MATLAB/R2013a/bin:$PATH


On Windows make sure the Matlab DLLs are in your “Path” environment variable. This version is not tested in Windows but should be possible to run with some debugging efforts.


  • Python

    Version 2.

  • Matlab

    Versions 2009a,2010a,2013a tested. Presumably any version?

  • Numpy

    Any version? tested on version 1.3.0.


The current version lets you transfer arrays of any rank between Python and Matlab using the following datatypes: Single and double precision floatpoint numbers. Integer numbers of different bit lengths (8-64) unsigned or signed. Complex numbers (single or double precision). Logical arrays. Any other types will probably fail or give unpredictable results.

Using pymatlab

First import:

>>> import pymatlab

Initialise the interpretor.

>>> session = pymatlab.session_factory()

Create an numpy-array to start the work.

>>> from numpy.random import randn
>>> a = randn(20,10,30)

Send the numpy array a to the MATLAB Workspace to the variable ‘A’

>>> session.putvalue('A',a)

Do something in matlab in MATLAB with variable A. Sucessfull commands return an empty string - if MATLAB generates an error the returning string holds the error message

Traceback (most recent call last):
RuntimeError: Error from Matlab: Error: MATLAB:UndefinedFunction with message: Undefined function or variable 'C'.

A trick to make larger scripts more failsafe with regards to syntax errors. Send a script to a string variable and run it with eval().

>>> mscript = """D = A
... for i=1:10
...    D = 2*D
... end
... """
>>> session.putvalue('MSCRIPT',mscript)

To retrive the variable back to python:

>>> b = session.getvalue('B')
>>> (2*a==b).all()

If you want to explicitly close the connection to the interpreter delete the instance. Normally Matlab will be close when the session variable runs out of scope.

>>> del session

Bugs, support and feature requests

All bug reports, feature requests or support questions are directed to to

Please consider to support us in our efforts by donating to pymatlab. Your donations will be used to buy hardware and software licenses to be able to continue to develop this package.


2013-10-24 pymatlab 0.2.3

Bugfix. Confirming the GPLv3 licence.

2013-10-21 pymatlab 0.2.2

Now integers of bitlength 8-64 signed or unsigned, single and double precision floats, complex numbers (single,double) and logical arrays are supported.

2013-10-15 pymatlab 0.2.1

Added support for int16, int32, int64, float32 and float64 matrices of any rank. Squeezing of matrices is done automatically. Added a convenience ‘session factory’ to create a session and start matlab.

2011-11-01 pymatlab 0.2.0

A ctypes implementation. This makes it easier to run and install because of no need for compilation.

2010-04-18 pymatlab 0.1.3

Run now throws exception RuntimeError on erros. A critical bug was fixed concernings numpys C memory alignment and MATLAB’s Fortran memory alignment in matrices.

2010-04-09 pymatlab 0.1.2

Bugfixes for 32-bit machines. Closed some memory leaks.

2010-02-26 pymatlab 0.1.1

Added the missing file to include the docs/ directory. And some small changes in the README.txt to work with restructured text.

2010-02-25 pymatlab 0.1.0

This first version om pymatlab including lots of potential memory leaks. The following features ships with this version:

  • running commands in the Matlab command interpretor.

  • Placing double precision matrices with arbitrary dimensions on the MATLAB workspace from numpy arrays.

  • Retrive double precision matrices from MATLAB workspace to numpy arrays.

  • Place string variables on MATLAB workspace.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymatlab-0.2.3.tar.gz (21.9 kB view hashes)

Uploaded Source

Built Distribution

pymatlab-0.2.3-py2.7.egg (20.9 kB view hashes)

Uploaded Source

Supported by

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