Main Content

Write MATLAB Code for Strongly Typed Interface

When creating a strongly typed C++ interface to MATLAB® functions or classes, you can stipulate how to represent MATLAB data types in C++ by using standard and custom data type mappings between MATLAB and C++. To specify the data type requirements, use an arguments block within a MATLAB function or a properties block and arguments block within a MATLAB class. For example, if your C++ application code uses a float data type to represent a real scalar double value, its equivalent representation in MATLAB is (1,1) single {mustBeReal}.

MATLAB Function with Strongly Typed Data

This strongly typed MATLAB function specifies data type requirements using an arguments block.

function r = stronglyTypedFun(num)
arguments
    num (1,1) single {mustBeReal}
end
r = magic(num);

For details, see Data Type Mappings Between C++ and Strongly Typed MATLAB Code.

When you compile a strongly typed MATLAB function, class, or package, MATLAB generates a C++ header (.hpp file). To generate the header file from the MATLAB command prompt, call matlab.engine.typedinterface.generateCPP.

The generated header file:

  • Maps strongly typed MATLAB data types to C++ data types.

  • Contains C++ namespaces that correspond to MATLAB package directories of the same name.

  • Contains C++ classes that correspond to MATLAB classes of the same name.

  • Contains public C++ methods that correspond to the public methods of MATLAB classes. The method names are unchanged and can be used as is in the C++ application code. These aligned names eliminate the need for intermediate layer top-level functions that call the class methods through an feval function execution.

  • Contains C++ get and set methods for public properties of MATLAB classes. The property names of MATLAB classes are prepended with get or set. For example, if the property name in a MATLAB class is UpperLeft, the corresponding C++ method names are getUpperLeft and setUpperLeft.

The strongly typed MATLAB class in this table specifies data type requirements using a properties block and arguments blocks. The table shows how the strongly typed MATLAB class in a shapes package is translated into a C++ header file.

MATLAB Class with Strongly Typed Data

Strongly Typed MATLAB ClassSnippet of C++ Header File
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

        // properties
        shapes::MyPosition getUpperLeft() {
            // code
        }
        void setUpperLeft(shapes::MyPosition obj) {
            // code
        }
        shapes::MyPosition getLowerRight() {
            // code
        }
        void setLowerRight(shapes::MyPosition obj) {
            // code
        }

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

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

    };
}

Sample C++ Application Code Snippet

Include the generated header file (.hpp file) in the C++ application code by using #include directives. You can then compile and run the application.

#include "MatlabEngine.hpp"
#include "shapes.hpp" //header file generated by matlab.engine.typedinterface.generateCPP

int main() {
    // Connect to MATLAB
    std::shared_ptr<matlab::engine::MATLABEngine> matlabPtr = matlab::engine::startMATLAB();

    // application specific code that relies on the generated header
    shapes::MyPosition p1(matlabPtr);
    ...
    shapes::MyRectangle r1(matlabPtr);
    ...
}

Tip

  • When writing C++ application code, you must include the header file (.hpp file) generated by the matlab.engine.typedinterface.generateCPP function and the MatlabEngine.hpp header file by using #include directives.

    For examples building engine applications, see Create C++ Header File from Strongly Typed MATLAB Function.

  • Your MATLAB code must be strongly typed to leverage all the features of the interface. Otherwise, data type mapping between MATLAB and C++ is absent, and you cannot use native C++ data types.

  • During generation of the strongly typed interface, the matlab.engine.typedinterface.generateCPP function retrieves type information from arguments and properties blocks. The retrieved information is array size, type, and whether it is a real number.

See Also

| |

Related Topics