Main Content

Map MATLAB Classes and Functions to C++

This guide details how MATLAB® classes and functions map to their corresponding C++ classes and functions. Through the lens of an example involving MATLAB classes named MyPosition and MyRectangle that are part of package +shapes, and a MATLAB function called calculatearea, we explore the C++ header file (libshapesv2.hpp) generated by the compiler.build.cppSharedLibrary function that forms the interface between MATLAB and C++.

At a high level:

  • MATLAB data types are mapped to C++ data types based on type specification in arguments and properties blocks.

  • MATLAB packages are mapped to C++ namespaces of the same name.

  • MATLAB classes are mapped to C++ classes of the same name.

  • Public methods of MATLAB classes maps to public C++ methods of the same name.

  • Properties of a MATLAB class are replicated as getter and setter functions in the C++ class. The property names are prepended with get and set to form the getter and setter functions.

 +shapes (Package)

 MyPosition.m (Class)

 MyRectangle.m (Class)

 calculatearea.m (Function)

 libshapesv2.hpp (C++ Header)

Header Mapping Excerpt

classdef MyRectangle

    properties
        UpperLeft  (1,1) shapes.MyPosition
        LowerRight (1,1) shapes.MyPosition
    end
    methods
        function R = enlarge(R, n)
            arguments
                R (1,1) shapes.MyRectangle
                n (1,1) double {mustBeReal}
            end
            % code
        end
        function R = show(R)
            arguments
                R (1,1) shapes.MyRectangle
            end
            % code
        end
    end
end
namespace shapes {
    class MyRectangle : public MATLABObject<MATLABControllerType> { 
    public:

        // constructors
        MyRectangle : MATLABObject() {}

        // code

        // getter and setter property methods
        shapes::MyPosition getUpperLeft() {
            // code
        }
        void setUpperLeft(shapes::MyPosition obj) {
            // code
        }
        shapes::MyPosition getLowerRight() {
            // code
        }
        void setLowerRight(shapes::MyPosition obj) {
            // code
        }

        // public methods
        matlab::data::Array show() { 
            // code
        }

        matlab::data::Array enlarge(double n) { 
            // code
        }

    };
}

MATLAB Package, Classes, and Function

+shapes

This is a MATLAB package containing two classes: MyPosition and MyRectangle.

MyPosition

This class is part of the shapes package and represents a position in two-dimensional space. It contains two properties: X and Y, both are real double values.

MyRectangle

This class is also part of the shapes package and it represents a rectangle. It contains two properties: UpperLeft and LowerRight which are instances of the shapes.MyPosition class. The class also has two methods: enlarge and show. The enlarge method enlarges the rectangle by a factor n, adjusting the UpperLeft and LowerRight positions. The show method displays the current values of UpperLeft and LowerRight positions.

calculatearea

The function calculatearea takes an instance of shapes.MyRectangle as an argument and calculates the area of the rectangle.

Mapping in C++

The C++ header file provides interfaces to the MATLAB MyPosition and MyRectangle classes and the MATLAB function calculatearea. This allows for interaction between C++ and MATLAB.

namespace shapes

The MATLAB shapes package is mapped to a C++ namespace of the same name.

shapes::MyPosition

The C++ shapes::MyPosition class represents the MATLAB MyPosition class. It includes getter and setter methods for the properties X and Y which correspond to the properties in the MATLAB class. They are: getX, setX, getY, setY. The constructor creates an instance of the MATLAB MyPosition class.

shapes::MyRectangle

The C++ shapes::MyRectangle class represents the MATLAB MyRectangle class. It includes getter and setter methods for the properties UpperLeft and LowerRight which correspond to the properties in the MATLAB class. They are: getUpperLeft, setUpperLeft, getLowerRight, setLowerRight. The constructor creates an instance of the MATLAB MyRectangle class.

The shapes::MyRectangle class also includes methods for show and enlarge which correspond to the methods in the MATLAB class. The methods make use of the MATLABControllerType instance to execute the MATLAB functions using the feval method.

calculatearea

The calculatearea function corresponds to the MATLAB calculatearea function. The function takes an instance of shapes::MyRectangle as an argument and calculates the area of the rectangle. The calculation is performed by calling the MATLAB calculatearea function using the feval method of MATLABControllerType.

See Also

| |

Related Topics