C++ MEX files
Nexus loads MEX files that MATLAB built against its C++ interface
(matlab::mex::Function and the matlab::data
array classes) as they are, next to the C MEX files it already runs. You
do not recompile them. This page lists what is supported and where the
behaviour differs. Tracking: nexus#1225 (implementation) and nexus#1234
(remaining gaps).
What is supported
- Files.
.mexa64(Linux) and.mexw64(Windows) built by MATLAB with a C++ MEX entry point (MexFunctionin a class derived frommatlab::mex::Function). Put the file on the path and call it like any function; the extension decides nothing else. - Arrays. The numeric classes (all integer widths,
single, double, logical), complex values, N-d and empty arrays, char
and string arrays (a missing string is an error, see below), structs,
cells and sparse matrices, through
TypedArray,CharArray,StructArray,CellArray,SparseArrayand the factory. Errors thrown from the file as C++ exceptions reach your script as errors with the exception text. - Calling back.
feval,eval,getVariable,setVariable("base","global"and the caller's workspace),getFunctionName, andmexLock/mexUnlock(they lock and unlock the MEX function itself, somislockedreports it andclear mexleaves it loaded), the asynchronous forms (see below) andgetProperty/setPropertyon MATLAB objects (value objects and handle objects, one element of an object array picked by index). - Windows. The file imports
libMatlabDataArray.dll,libmex.dllandlibmx.dllby name. Nexus installs DLLs of those names in its program directory that forward to its own implementation, so Windows resolves the imports without any setup. This part is built for Windows but unproven at runtime: the C++ MEX tests are Linux only (! ispc ()), and loading a MATLAB-built.mexw64through the forwarder DLLs has not been confirmed on a Windows run; see nexus#1234.
How it works
A C++ MEX file contains the C++ classes itself and calls the host
through plain C functions (get_function_ptr,
cppmex_*, mexApi*). Nexus provides those
functions in libnexusmex. The loader notices the file's
mexCreateMexFunction and mexFunctionAdapter
entries, and calls the adapter in place of the empty C
mexFunction the file also exports. Arguments are converted
to the data-array form on the way in and back on the way out. On Linux
the install carries empty libMatlabDataArray.so,
libMatlabEngine.so, libmx.so,
libmex.so and libmat.so in
lib/nexus/<version>/mexcompat; they only make the
names the file depends on resolve to libnexusmex.
Differences from MATLAB
- Text is UTF-8 inside Nexus. The data API's
characters are UTF-16 code units. A single row of text is converted
from UTF-8 on the way in and back on the way out, so non-ASCII text
arrives as the right code units; other shapes (several rows, N-d) are
passed byte for byte, and a UTF-16 code unit above 0xFF in such a
value is replaced by
?. - Asynchronous calls.
fevalAsync,evalAsync,getVariableAsync,setVariableAsyncand the property forms queue a task, as in MATLAB. The task runs when the file waits for its result (future.get(), or a wait on the future), or after the file's call returns, which is when MATLAB itself gets to it. A task that has not run yet can be cancelled; its future then holds a "MATLAB command was cancelled." error. Cancelling a task that has already run returns false. Arguments are copied when the task is queued. - Object properties.
getProperty/setProperty(and the async forms) work through the interpreter's subsref / subsasgn.setPropertyreturns the modified object; a value object is copied, a handle object is changed in place. An unknown or read-only property, or a first argument that is not an object, raises aMATLABExceptionin the file; the interpreter's message text for the property errors differs from MATLAB's. Other class instances cannot be passed to the data API: that raises "MATLAB objects are not supported by Nexus MEX." - Missing strings. A
string(missing)argument cannot be read as text and raises a "missing string" error. - Complex layout. C++ MEX files always use the interleaved complex layout, as in MATLAB.
- Workspace of
eval. As in MATLAB, the engine'sevalruns in the workspace of the function that called the MEX file, not in the base workspace. - Threads. The engine (
feval, variables, properties, the async forms,mexLock/mexUnlock) belongs to the thread that called your function. Called from a thread the file started, these fail with the engine's thread error and the identifierNexus:cppmex:wrongThread, as in MATLAB; setting outputs from another thread fails the same way. The data array functions may be used from any thread on distinct arrays. Finish (join) your own threads beforeMexFunctionreturns. - Sparse results. A sparse array handed back by the file comes out as a sparse double, complex or logical matrix. Sparse arguments are supported for double, complex double and logical matrices; a sparse argument of another class raises an error.
The behaviours above that match MATLAB are those the test file
test/mex/mexcompat-cpp-api.tst checks: its expected values
were produced by running the same fixtures under MATLAB R2026a.
Writing your own C++ MEX file
Nexus ships a clean-room header set for the same public names
(mex.hpp, mexAdapter.hpp,
MatlabDataArray.hpp, MatlabEngine.hpp) under
include/nexus/cppmex. nexus-mex file.cpp
adds that directory to the include path, compiles as C++17, and links
libnexusmex plus mexfilerequiredapiversion.cpp.
A MATLAB-built C++ MEX still loads without a rebuild; the headers are
for building one against Nexus. ArrayFactory::createEnumArray
and EnumArray store the class and member names; Nexus
resolves them when the array is returned, so an unknown class or member
fails at that point, as the cppmex_idiom_data enum commands
expect. ObjectArray and
ArrayFactory::createObjectArray wrap the object-array
table entries (ids 120–127). The headers were written from Nexus's
own function table, not from MathWorks header source. The older
MATLAB-only rebuild of the test fixtures remains
test/mex/build-cppmex-fixtures.sh. For C code written
against mex.h use nexus-mex, as described on
the Toolboxes page.
Example sources
Small, commented C++ MEX examples are in the repository under
examples/cpp-mex/:
hello.cpp: minimal function returning a stringvecsum.cpp: sum and running sum withTypedArray<double>structbuild.cpp: building a structcellstrings.cpp: cell array of strings in and outcallfeval.cpp: callingfevalfrom C++errors.cpp: throwing and catching errors
Troubleshooting
- "cannot open shared object file: libMatlabDataArray.so"
(Linux). The stub libraries are missing or not where the
loader looks: check that
lib/nexus/<version>/mexcompat/libMatlabDataArray.soexists in your installation. You should not needLD_LIBRARY_PATH, and you should not point it at a MATLAB installation: the real MathWorks libraries would be picked up instead of Nexus's. - Unresolved symbol at load. A name the file imports is not provided; report the name in nexus#1234.
- Error "Nexus:cppmex:wrongThread". The file called
the engine or set outputs from a thread it started; do that from the
thread that runs
MexFunction. - Windows: "The specified module could not be
found".
libMatlabDataArray.dllorlibmex.dllfrom the Nexus program directory is not reachable; run from the installed Nexus, not a copy of the executable alone.