SwigMatlabPlus
(c) 2001 Sumit Basu, MIT Media Laboratory
Overview
SwigMatlabPlus is a simple extension of SWIG
(Simple Wrapper and Interface Generation). This allows you to take C++ code
(classes and all) and easily export them to MATLAB. For instance, consider the
following class:
class foo {
foo();
~foo();
int x;
int bar(double y);
};
You can access it from matlab with the following:
>> q= foo('new_foo')
q = _1023a_foo_p;
>> foo('foo_bar', 3)
ans=7
I can't take all the credit for this - I borrowed heavily from the MATLAB example
in the SWIG source distribution. That version was useful, but could only deal
with scalar arguments and did not have C++ class support. I also added support
for vectors/matrices through the helper functions described below.
Features
- Simple interface/code generation for converting C++ modules to MATLAB. No
more messing with prhs[0], plhs[0], etc.
- Class support - access C++ objects, member variables, and methods from MATLAB.
- Support for vectors and matrices through the fm*Matrix classes (written
by Brian Clarkson). Support
exists for double, float, int, short, and unsigned char matrices (and it's
easy to add others). This is somewhat inefficient in that fmMatrix is row-major
(more natural in C) while MATLAB matrices are column major. Furthermore, matrices
that live in the MATLAB side need to have their storage allocated by mxCalloc/etc.,
otherwise bad things happen. However, we're already using fmMatrix classes
for a lot of other code, so it was more convenient.To keep this reasonably
efficient, I provide helper functions to convert between MATLAB matrices and
fmMatrices, so it's up you to decide when to incur the cost of the conversion.
Feel free to add your own (hopefully more efficient) matrix classes -- it's
not very difficult.
- Passthrough of the mxArray * class into C++ (see the matlab.map file).
Software
The package swigmatlabplus.zip has a number of components:
- swigmatlabplus.dsw - the workspace (for Visual C++ 6) to build everything
else. You'll have to download the SWIG source to build SwigMatlabPlus, but
the rest of the stuff will build as long as you've got MATLAB. You will need
to change the settings to correspond to your source/lib directories, of course.
- swigmatlabplus.exe - the wrapper generation mechanism
- matfm.dll - helper functions that allow translation from _2353a_fmMatrix_p
to a MATLAB matrix and vice versa. These are accessed through the matlab wrapper
functions: mat2fm*Matrix.m, mat2fm*Vector.m, fm*Vector2mat.m, and fm*Matrix2mat.m.
(The source for this can be found in matfm.dsp)
- test.h, test.cpp, test.i, test_wrap.cpp, in test.dsp - a simple example
that shows how to use this stuff.
Tutorial
An EXTREMELY terse tutorial based on the test* module is coming soon.
Related Information
Building
MEX files in Visual Studio (i.e., circumventing the mex script). Note: This
is for MATLAB 6 or greater.
Running swigmatlabplus in Visual Studio
Author
Sumit Basu. Developed 8/01 at the MIT Media Laboratory.
Acknowledgements
Thanks to the SWIG team, who did most of the work - this is just an illustration
of how easy it is to modify their code for other target languages. Also thanks
to Brian Clarkson, who provided the matrix code and had helpful design advice.