We often need to create programs using a mixture of languages such as C++ and MATLAB.  Each has its own strengths and weaknesses but creating a single program in a mix of languages is typically difficult.  MATLAB has had, forever, a MEX interface that allowed C and C++ functions to be called from MATLAB but it is cumbersome to write MEX files and only simple data types (double and integer arrays) can be passed back and forth.

MATLAB R2019a introduced a new mechanism to link with C++ code which allows user-defined C++ classes to appear as MATLAB objects and their C++ methods to be invoked just as they would be for first-class MATLAB objects.  MEX files are no longer required.

Consider the following very simple example:


    doublex, y, width, height;
    Rectangle(doublex, doubley, doublewidth, doubleheight);
// rectangle.cpp

#include "rectangle.h"

Rectangle::Rectangle(doublex, doubley, doublewidth, doubleheight)
  this->x = x;
  this->y = y;
  this->width = width;
  this->height = height;

  return this->width * this->height;

We need to an object file or library, so we first need to compile the source code in this example. We can do this easily enough from the MATLAB prompt

>> !gcc -c rectangle.cpp

Next we generate a “predigested” definition of the class definition based on the interface file and the object code:

>> clibgen.generateLibraryDefinition('rectangle.h', 'Libraries', 'rectangle.o')Using Xcode Clang++ compiler.
Generated definition file definerectangle.mlx and data file 'rectangleData.xml' contain definitions for 4 constructs supported by MATLAB.
Build using build(definerectangle).

and then we build a dynamically loadable interface (this is on my Mac)

>> build(definerectangle)
Building interface file 'rectangleInterface.dylib'.
Interface file 'rectangleInterface.dylib' built in folder '/Users/corkep/proj/matlab/new_in_19a/rectangle'.
To use the library, add the interface file folder to the MATLAB path.

These steps only need to be done once and the interface files are created in a subfolder called rectangle – let’s place that on the MATLAB path

>> addpath rectangle

Now we can invoke the constructor which calls my C++ code

>> r = clib.rectangle.Rectangle(1, 2, 3, 4)
r = 
  Rectangle with no properties.

and we have a new MATLAB object with a bunch of methods

>> methods(r)
Methods for class clib.rectangle.Rectangle:
Area       Rectangle eq  ge   gt le  lt ne         
Methods of clib.rectangle.Rectangle inherited from handle.

It’s a handle subclass which means the object will be passed by reference. We can see it has some standard MATLAB handle class methods like eq etc. but also my C++ method for computing area which is invoked in normal MATLAB fashion

>> r.Area
ans =

The MATLAB class is simply an abstraction of the underlying C++ class.

All of this makes is really easy to exploit existing packages written in C++ while keeping the convenience of the interactive MATLAB development process, high-quality graphics, powerful toolboxes and interactive notebooks (called LiveScripts in MATLAB speak).  Interestingly all of this works in MATLAB Online as well (the cloud version of MATLAB which runs under Linux on AWS) which means you can share your code to a colleague and have it appear in their MATLAB cloud drive (a bit like shared folders on Dropbox) from where it can be run.

Integrating C++ into MATLAB