Main Content

matlab.engine.typedinterface.generateCPP

Generate C++ code interface for MATLAB packages, classes, and functions

    Description

    example

    matlab.engine.typedinterface.generateCPP(headerFile,Name=Value) creates a C++ header file from one or more MATLAB® packages, classes, and functions. You must specify at least one Packages, Classes, or Functions name-value argument. Use the header file to build a C++ engine application.

    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.

    For more information, see Write MATLAB Code for Strongly Typed Interface.

    Examples

    collapse all

    Create a C++ header file myPkg.hpp from MATLAB classes Position and Rectangle in a shapes package.

    Create a package named shapes on the MATLAB path.

    Create a package file Position.m with these statements.

    classdef Position
        properties
            X   (1,1) double {mustBeReal}
            Y   (1,1) double {mustBeReal}
        end
    end

    Create a package file Rectangle.m with these statements.

    classdef Rectangle
        properties
            UpperLeft  (1,1) shapes.Position
            LowerRight (1,1) shapes.Position
        end
    
        methods
            function R = enlarge(R, n)
                arguments
                    R (1,1) shapes.Rectangle
                    n (1,1) double {mustBeReal}
                end
    
                % code
            end
    
            function R = show(R)
                arguments
                    R (1,1) shapes.Rectangle
                end
    
                % code
                disp("Upper left position (" + R.UpperLeft.X + "," + R.UpperLeft.Y + ")");
                disp("Lower right position (" + R.LowerRight.X + "," + R.LowerRight.Y + ")");
            end
        end
    end

    Generate the C++ header file myPkg.hpp.

    matlab.engine.typedinterface.generateCPP("myPkg.hpp",Packages="shapes")
    2 class(es) and 0 function(s) written to myPkg.hpp

    You can use the header file myPkg.hpp to build a C++ engine application.

    Input Arguments

    collapse all

    Name of the C++ header file created by the function, specified as a string scalar or a character vector. The name can include a relative or absolute path.

    Example: "myPkg.hpp"

    Example: "myFolder\myPkg.hpp"

    Example: "C:\myFolder\myPkg.hpp"

    Name-Value Arguments

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Example: matlab.engine.typedinterface.generateCPP("myPkg.hpp",Classes=["Position","Rectangle"],DisplayReport=true,SaveReport="report.log") generates the C++ header file myPkg.hpp using the Position and Rectangle classes, displays generation messages, and saves the log as report.log.

    Note

    You must specify at least one Packages, Classes, or Functions name-value argument.

    MATLAB packages to include in the C++ code interface, specified as a string array.

    Example: Packages="shapes"

    MATLAB classes to include in the C++ code interface, specified as a string array. The function supports user-authored classes and classes inherited from other user-authored classes. For enumeration classes, the function generates a simple C++ scoped enum, which means properties and methods of the class and default values of the enumeration are not generated into C++.

    Example: Classes=["Position","Rectangle"]

    MATLAB functions to include in the C++ code interface, specified as a string array. The function supports user-authored functions.

    Example: Functions=["show","enlarge"]

    Option to display generation messages, specified as a numeric or logical 1 (true) or 0 (false).

    Log file name, specified as a string scalar. The name can include a relative or absolute path. The option creates a text file, so you can specify an appropriate file extension, but that is not required. The file contains detailed information about the matlab.engine.typedinterface.generateCPP function call and the generated structures.

    Example: SaveReport="report.log"

    Version History

    Introduced in R2022a