matlab.engine.typedinterface.generateCPP - Generate C++ code interface for MATLAB namespaces, classes, and functions - MATLAB (original) (raw)

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

Since R2022a

Syntax

Description

matlab.engine.typedinterface.generateCPP([headerFile](#mw%5Ff3ccb7c6-9fda-4c4a-b50b-448bcda4b424),[Name=Value](#namevaluepairarguments)) creates a C++ header file from one or more MATLAB® namespaces, classes, and functions. You must specify at least oneNamespaces, Classes, or Functions name-value argument. Use the header file to build a C++ engine application.

The generated header file:

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

example

Examples

collapse all

Create Header File from MATLAB Namespace

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

Create a namespace named shapes on the MATLAB path.

Create a namespace file Position.m with these statements.

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

Create a namespace 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",Namespaces="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

headerFile — Name of C++ header file

string scalar | character vector

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. Best practice is to place C++ code files in separate folders from MATLAB files.

Example: "myFolder\myPkg.hpp"

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

Name-Value Arguments

Specify optional pairs of arguments asName1=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 thePosition and Rectangle classes, displays generation messages, and saves the log as report.log.

Note

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

Namespaces — MATLAB namespaces

string array

Since R2024a

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

Example: Namespaces="shapes"

Classes — MATLAB classes

string array

MATLAB classes to include in the C++ code interface, specified as a string array. The function supports user-authored value and handle 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"]

Functions — MATLAB functions

string array

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

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

DisplayReport — Option to display generation messages

false or 0 (default) | true or 1

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

SaveReport — Log file name

string scalar

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 thematlab.engine.typedinterface.generateCPP function call and the generated structures.

Example: SaveReport="report.log"

Version History

Introduced in R2022a

expand all

R2024a: Support for handle classes

You can include handle classes in a strongly typed C++ interface. The function creates code with handle copy behavior. That is, functions and methods can modify the input data in the calling workspace without making copies.

The function does not generate code to support dynamic properties, theset and get behavior for graphics handles, listener workflows, and the findprop, findobj, anddelete handle methods.

R2024a: Support for comparison operators

The function generates C++ comparison methods and the isvalid function as well as comparison operators for both handle and value classes which implement the eq, ne, lt,gt, le, and ge methods in MATLAB.

Code generated for comparison operators like == have a boolean return type. If you, for example, override the eq function in MATLAB with a nonboolean return value, then you must use the eq method in your application instead of ==.

R2024a: Packages name-value argument renamed to Namespaces

The name of the Packages name-value argument is now Namespaces. The behavior of this name-value argument remains the same. There are no plans to remove support for existing references to the argument.