A C++ wrapper class for MATLAB's mxArray type
Updated 8 Aug 2011

View License

arrayadapt provides a convient way to access mxArrays in MATLAB's .mex files.
The 'ArrayAdaptor' class template can be specialzed for one of the builtin
numeric types and a fixed number of dimensions. It allows for subscripted
indexing in additon to using C's []-indexing style.
An array which is constructed to be n-dimensional will only take 'n' arguments for
specifying the size of each dimension in the constructor. Specifying a different
number will result in a compiler-error. Furthermore, also the indexing
operator will take exactly 'n' arguments. Providing more or less than 'n' indices
will result in a compiler-error, again. One source of headache eliminated.

Currently, one- through three-dimensional arrays of any numeric type can be
wrapped, but extending to an arbitrary number of dimensions can be easily done
by implementing two simple class templates. One that takes care of construction
and one that encapsulates element access.

If debug mode is active for your mex-file, bounds-checking on element access
and type-checking on construction is done (at runtime, of course).

Since arrayadapt only provides a different interface to the underlying mxArray
it will never interfere, in any way, with existing code that changes the data
by other means (direct access, other wrappers, etc).

Along with arrayadapt comes an 'mx_traits' class template which is currently
used to provide a consistent way to get the mxClassID associated with the
numeric types in C.

On performance:
Any decent compiler should be able to strip away the arrayadapt extras completely.
On my systems (tested on gcc 4.5 and 4.6) there is no trace left of it in the
resulting assembly. No function calls, no classes, no nothing.

All files are also available directly via github (

Cite As

Armin Schmidt (2024). arrayadapt (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2011a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Performance and Memory in Help Center and MATLAB Answers
Tags Add Tags

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes

Dropped the non-singleton dimension constraint