Replacement for mwArray using Matlab Engine (Scripts) Publisher's description
from David Johnson
Update 2011: An example application is included which shows a 2D plot using Matlab formulas
Update 2011: An example application is included which shows a 2D plot using Matlab formulas. Requires MFC. Visual Studio 2008 is recommended, but the project should work in other versions.
Another update is more correct support for multi-threading in MxWrapper.
As part of maintaining a large, legacy C++ application, I needed the to continue to use the functionality of mwArray. Unfortunately, Mathworks discontinued mwArray as a useful tool in MATLAB 6.5 (R13). You would know if your program needs the old mwArray if you see #include "matlab.hpp" anywhere in your code. You cannot perform any mathematical operations on mwArray in the current version of Matlab.
My solution was to write a smart pointer class to hold an mxArray and then send it to the Matlab Engine to perform computations. mxWrapper's "smart" pointer simply frees the mxArray pointer when in the mxWrapper destructor function. mxWrapper also implements matrix slicing (using mxWrapperColon()) in both assignments and sub-array selection. It's still a useful and powerful tool for computation. For example, see these tutorials:
"Solving Engineering Problems Using MATLAB C++ Math Library" by By A. Riazi | 22 Jul 2003
"Using the C++ Math Library" (old Matlab 6.1 documentation):
mxWrapper makes this fast and easy, and it has most of the functionality of mwArray. In fact, the same code will compile if you do something like this:
typedef mxWrapper mwArray;
#define colon mxWrapperColon
I purposely put mxWrapper in its own namespace so that I could move individual functions (sin, cos, etc) from mwArray to mxWrappers one at a time, maintaining compatibility.
The attached files will compile under both MATLAB 6.1 and 2010a. Therefore I was able to make a gradual transition to the new Matlab engine by hiding it behind mxWrapper.
I wrote some example functions in mxTestWrappers() and mxTestEngine(). For example:
mxWrapper e = mxWrapperColon(3, 7);
mxWrapper g = e( find( e >= 4 ) );
e( f ) = 0;
e.put(ep, "e"); // now do something in Matlab with it
cout <<"mxTestWrappers: g = e( f )" << g;
I would not suggest writing any new, large C++ programs based on this library because there are many better alternatives (ITK, STL, boost multiarrays). I am making this available to others so that they can move their legacy programs to the Matlab Engine more easily. The biggest selling point is probably the automatic memory management of the mxArray pointer using the Resource Acquisition Is Initialization pattern.
Some caveats: I did not implement the "copy on write" semantics of mwArray, and I did not implement sparse arrays. mxWrappers cannot share ownership of an mxArray pointer (the memory is automatically duplicated instead). Not all of the old mwArray functions are implemented, but if you follow the example of mxWrapperNS::fft() you should see how easily you can add your own functions. I covered all of the functions I needed (repmat, sum, cumsum, transpose, linspace, etc.).
System Requirements:MATLAB 7.10 (2010a)
Program Release Status: New Release
Program Install Support: Install and Uninstall