PyMat - An interface between Python and MATLAB
|Functions | Limitations | Examples | Installation | Copyright | To Do | History||
July 9, 1999
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:
On the Win32 platform, the optional parameter
is always ignored. On UNIX platforms, it indicates the method
of starting MATLAB. Quoting from the documentation for the
function from the MATLAB API reference manual:
On UNIX systems, if
startcmdis NULL or the empty string,
engOpenstarts MATLAB on the current host using the command
startcmdis a host-name,
engOpenstarts 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'\""
startcmdis any other string (has white space in it, or nonalphanumeric characters), the string is executed literally to start MATLAB.
On UNIX systems,
engOpenperforms 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 (
rshfor remote execution).
Under Windows on a PC,
engOpenopens 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:
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.
nameparameter 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)
dataparameter can be one of three things:
array()function) and that object is instantiated in MATLAB's workspace.
The following limitations apply to the current version of PyMat:
Here is a simple example that computes the Discrete Cosine
Transform of a short NumPy array using MATLAB's
>>> 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)
Simply place the
pymat.pyd file somewhere on
your Python search path. Also ensure that MATLAB's
directory is somewhere on your DOS path, or else that you have
files somewhere on this path.
pymat.cpp file to your
source subdirectory. You may need to rename
pymat.cc so that C++ compilers will recognize
You will need to add a line to your
file to indicate the presence of this new module. You will need
-I switches on this line to tell the
compiler where to look for MATLAB's
file (usually in the
of the MATLAB root installation directory) and NumPy's
include file. You will also need the
-L switch to
indicate the location of MATLAB's shared libraries, and
switches to include those libraries. For example, my line in
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:
Finally, you need to set an environment variable telling MATLAB
where it can find its shared libraries. On most systems, this
LD_LIBRARY_PATH. On HP700 systems this
SHLIB_PATH. On IBM RS/6000 systems this
Note that some makefiles are included in the PyMat distribution that may simplify this process. Also note that Matlab version 6.1 seems to no longer have the "libmi.so" library so you should remove "-lmi" from the compilation options.
Copyright © 1998,1999 Andrew Sterian. All Rights Reserved. mailto: email@example.com
Copyright © 1998,1999 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.
Version 1.02 -- July 9, 1999
Version 1.01 -- March 2, 1999
Version 1.0 -- December 26, 1998