Matlab C++ Integration
I’m currently working on integrating some Matlab Algorithms into a larger C++ project using the Matlab Compiler. It works rather well however I stumbled across a couple of quirks and caveats. First two facts that make integrating Matlab difficult: in Matlab indexes start at 1 and Matlab arrays are column-major ordered. As a result if you want to use data fields in both C/C++ and Matlab you have to transpose the fields. If the fields contain indexes you might also have to increment and decrement the values of the fields.
Another caveat I found is also related to array indexes. When using a Matlab function that takes as parameters matrix indexes you might think of creating an mwArray of type mxUINT32_CLASS. Wrong. You have to pass indices – as strange as it sounds – as doubles so you should use mxDOUBLE_CLASS.
One custom Matlab function I wanted to use in C++ took as an argument a cell array. A cell array is an array containing other arrays. In Matlab you create a cell array like this:
a = magic(5); b = ones(3); z{1} = a; z{2} = b;
If you look at z now you will get something like this:
z = [5x5 double] [3x3 double]
An array of arrays. Easy. Well if you want to construct a cell array in C++ you have to do something like this:
mwArray a(5, 5, mxDOUBLE_CLASS); mwArray b(3, 3, mxINT16_CLASS); mwArray z(1, 2, mxCELL_CLASS); z.Get(1,1).Set(a); // Sets z(1) = a z.Get(1,2).Set(b); // Sets z(2) = b
You can find the description on this page.
If you get used to those little quirks however the Matlab Compiler is a very powerful tool to include your crazy Matlab scripts in a stand alone C++ application. One of the next things I would like to investigate are free (open source) Matlab Compiler alternatives such as the Octave C++ integration and compiler.
No Comments
Sorry, the comment form is closed at this time.