A python 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 migrate from a large MATLAB codebase to python scripts - one step at a time - by using old MATLAB scripts.
The package uses Numpy’s ndarrays and translates them into MATLAB’s mxarrays using Numpy’s c-api and Matlab mx library. The interface to MATLAB’s workspace in done through MATLAB’s engine library.
Download
Downloading is possible from PyPi and SourceForge pymatlab files. Since pymatlab is hosted at SourceForge the latest development version is avalable from subversion.
Installing
When installing from source distribution the include_dir and library_dir in setup.cfg needs to be altered. Sample configurations for different platforms are avalable in the package.
Preparing to use pymatlab
On UNIX like systems let the system administrator add the MATLAB library directories in ldconfig. If you don’t have root access set the environmental variable LD_LIBRARY_PATH to correct path where pymatlab can find MATLAB’s libraries (libmx and libeng). On my system with MATLAB installed in /opt/matlab I have put this line in my .bashrc:
export LD_LIBRARY_PATH=/opt/matlab/bin/glnxa64:$LD_LIBRARY_PATH
On Windows make sure the Matlab DLLs are in your “Path” environment variable.
Requirements
Matlab
Versions 2009a and 2010a verified. Presumably any version?
Numpy
Any version? tested on version 1.3.0.
Limitations
The current version lets you transfer double precision matrixes of any rank. Any other types will probably fail and give unpredictable results.
Using pymatlab
First import:
>>> from pymatlab.matlab import MatlabSession
Initialise the interpretor, an optional argument is a string to launch matlab:
>>> session = MatlabSession() >>> session.close()
Now with optional parameters:
>>> session = MatlabSession('matlab -nojvm -nodisplay')
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
>>> session.run('B=2*A')
>>> session.run('C') Traceback (most recent call last): ... RuntimeError: Error from Matlab: Error: MATLAB:UndefinedFunction with message: Undefined function or variable 'C'. end.
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.putstring('MSCRIPT',mscript) >>> session.run('eval(MSCRIPT)')
To retrive the variable back to python:
>>> b = session.getvalue('B') >>> (2*a==b).all() True
Don’t forget to close MATLAB.
>>> session.close()
Bugs, support and feature requests
All request for support like bugfixing and installation support or feature requests are directed to the SourceForge tracker for pymatlab.
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.
HISTORY
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 MANIFEST.in 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for pymatlab-0.1.3-py2.6-linux-x86_64.egg
Algorithm | Hash digest | |
---|---|---|
SHA256 | 76f07e2a51d41a6c956c41ce1951f75a61e8093c10c6b4733bb6262837ceffb2 |
|
MD5 | e68e42d0e8dc16c3c04244356695542f |
|
BLAKE2b-256 | a5cf85bf3b5049e218e7c1d35d88464e75c28aa055699afb578d4e2001b67454 |
Hashes for pymatlab-0.1.3-py2.6-linux-i686.egg
Algorithm | Hash digest | |
---|---|---|
SHA256 | 98df85982a1798c8a1e0d50be4856b15b7da589c5593cf1441f47c9422c94359 |
|
MD5 | f81624c163b1a0c156823e8d86a6a299 |
|
BLAKE2b-256 | 17d3722d86343dbfe1f325f7e98b7b1dc68853d11c476b68e681be7f104ac70e |
Hashes for pymatlab-0.1.3-py2.5-linux-x86_64.egg
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d658fa6b1f538013539786f4e56174bcf3127c454092e35ddcde6e74f389daa |
|
MD5 | 6c026c1fd5373c5d8b1c706d7de9c126 |
|
BLAKE2b-256 | 3c08fe55d43c94e1c73ea19d7e7312ea51a9221b1166e2a1495d3687232d37a3 |
Hashes for pymatlab-0.1.3-py2.5-linux-i686.egg
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3e106d1a6a48cdd699ba258e37048af16ee2acfcd7af332da976512bab9b19cc |
|
MD5 | bc9161bbc0ab38e1f16f9d8bd0b3965a |
|
BLAKE2b-256 | c84826e98c4b75f0091d1b3409676a0b1ff65318da076418a35189144b0c0128 |