Replacement for mwArray using Matlab Engine

mxWrapper emulates the functionality of mwArray, a C++ library used for Matlab computation
597 Downloads
Updated 8 Jun 2011

View License

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.

http://www.mathworks.com/support/solutions/en/data/1-TOVEL/?1-TOVEL

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

http://www.codeproject.com/KB/cpp/matlab_cpp.aspx

"Using the C++ Math Library" (old Matlab 6.1 documentation):

http://www.socsci.umn.edu/doc/matlab/toolbox/mathlib/cppmathug/working5.html

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:

using mxWrapperNS::mxWrapper;
typedef mxWrapper mwArray;

using mxWrapperNS::mxWrapperColon;
#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.).

Cite As

David Johnson (2024). Replacement for mwArray using Matlab Engine (https://www.mathworks.com/matlabcentral/fileexchange/28331-replacement-for-mwarray-using-matlab-engine), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.1.0.0

An example application is included, and more correct support for multi-threading in MxWrapper is implemented.

1.0.0.0