PyMat - An interface between Python and MATLAB
Functions | Limitations | Examples | Installation | Copyright | To Do | History |
Version 1.0 December 26, 1998 |
NumPy is a set of numerical extensions for Python that introduces a multidimensional array type and a rich set of matrix operations and mathematical functions. Users who have MATLAB 5 installed, however, may wish to take advantage of some of MATLAB's additional functions, including the plotting interface. The PyMat module acts as an interface between NumPy arrays in Python and a MATLAB engine session, allowing arrays to be passed back and forth and arbitrary commands to be executed in the MATLAB workspace.
PyMat is usable on both UNIX and Win32 platforms, although there are some slight differences between the two (mainly due to MATLAB's limitations).
You can download the latest version of PyMat from:
http://www-personal.umich.edu/~asterian/Python
open([startcmd])
This function starts a MATLAB engine session and returns a handle that is used to identify the session to all other PyMat functions. The handle is of integer type.
On the Win32 platform, the optional parameter startcmd
is
always ignored. On UNIX platforms, it indicates the method of starting
MATLAB. Quoting from the documentation for the engOpen
function from the MATLAB API reference manual:
On UNIX systems, ifstartcmd
is NULL or the empty string,engOpen
starts MATLAB on the current host using the commandmatlab
. Ifstartcmd
is a host-name,engOpen
starts MATLAB on the designated host by embedding the specified hostname string into the larger string:
"rsh hostname \"/bin/csh -c 'setenv DISPLAY
hostname:0; matlab'\""
Ifstartcmd
is any other string (has white space in it, or nonalphanumeric characters), the string is executed literally to start MATLAB.On UNIX systems,
engOpen
performs the following steps:
- Creates two pipes.
- Forks a new process and sets up the pipes to pass stdin and stdout from MATLAB (parent) to two file descriptors in the engine program (child).
- Executes a command to run MATLAB (
rsh
for remote execution).Under Windows on a PC,
engOpen
opens an ActiveX channel to MATLAB. This starts the MATLAB that was registered during installation. If you did not register during installation, on the command line you can enter the command:
matlab /regserver
close(handle)
This function closes the MATLAB session represented by handle
.
eval(handle, string)
This function evaluates the given string in the MATLAB workspace. Essentially, it is as if you had typed the string directly in MATLAB's command window.
Note that this function always succeeds without any exceptions unless the handle is invalid, even if the evaluation failed in the MATLAB workspace!. You are responsible for verifying successful execution of your code.
get(handle, name)
This function retrieves a matrix from MATLAB's workspace and
returns a NumPy array with the same shape and contents. The name
parameter specifies the name of the array in MATLAB's workspace.
Currently, only one-dimensional and two-dimensional floating-point arrays (real or complex) are supported. Structures, cell arrays, multi-dimensional arrays, etc. are not yet supported. On UNIX systems, this function can also be used to retrieve character strings, in which case a Python string object is returned. This functionality is not supported on Win32 (due to MATLAB restrictions).
put(handle, name, data)
This function places a Python object into MATLAB's workspace
under the given name. The data
parameter can be one of
three things:
array()
function) and that object is instantiated
in MATLAB's workspace.dct
function:
>>> import pymat >>> from Numeric import * >>> x = array([1, 2, 3, 4, 5, 6]) >>> H = pymat.open() >>> pymat.put(H, 'x', x) >>> pymat.eval(H, 'y = dct(x)') >>> print pymat.get(H, 'y') [ 8.57321410e+00 -4.16256180e+00 -1.55403172e-15 -4.08248290e-01 -1.88808530e-15 -8.00788912e-02] >>> pymat.close(H)
Win32
Simply place the pymat.pyd
file somewhere on your
Python search path. Also ensure that MATLAB's BIN
directory
is somewhere on your DOS path, or else that you have copied MATLAB's
LIBENG.DLL
and LIBMX.DLL
files somewhere
on this path.
Unix
Add the pymat.cpp
file to your Modules
source subdirectory. You may need to rename pymat.cpp
to
pymat.cc
so that C++ compilers will recognize the extension.
You will need to add a line to your Modules/Setup
file to indicate the presence of this new module. You will need to
specify -I
switches on this line to tell the compiler
where to look for MATLAB's engine.h
include file
(usually in the extern/include
subdirectory of the
MATLAB root installation directory) and NumPy's arrayobject.h
include file. You will also need the -L
switch to indicate
the location of MATLAB's shared libraries, and -l
switches
to include those libraries. For example, my line in the Modules/Setup
file looks like this:
pymat pymat.cc -I$(prefix)/include -I$(matlab)/extern/include -I$(numpy)/Include -L$(matlab)/extern/lib/lnx86 -leng -lmx -lmat -lmi -lut
When recompiling Python you may need to manually specify a C++ compiler. For example, I found that I had to do the following:
make CCC=g++
Finally, you need to set an environment variable telling MATLAB where
it can find its shared libraries. On most systems, this variable is
LD_LIBRARY_PATH
. On HP700 systems this variable is
SHLIB_PATH
. On IBM RS/6000 systems this variable is
LIBPATH
.
Copyright © 1998 Andrew Sterian. All Rights Reserved. mailto: asterian@umich.edu
Copyright © 1998 THE REGENTS OF THE UNIVERSITY OF MICHIGAN. ALL RIGHTS RESERVED
Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notices appear in all copies and that both these copyright notices and this permission notice appear in supporting documentation, and that the name of The University of Michigan not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission.
THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION AS TO ITS FITNESS FOR ANY PURPOSE, AND WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.