Main Content

C++ Names That Are Invalid in MATLAB

MATLAB® automatically renames classes, member functions, non-member functions, and enumerations with C++ names that are invalid in MATLAB by using the matlab.lang.makeValidName function. Enumerants and data members with C++ names that are invalid are not automatically renamed.

Rename Manually

A publisher can rename a class, enumeration,​ or non-member function in the library definition file. Renaming C++ namespaces,​ the outer (enclosing) class for nested classes, member functions,​ data members,​ enumerants, or the MATLAB namespace (interface name) is not supported.

For example, MATLAB converts the class name _myclass in library mylib to x_myclass. To use the class in MATLAB, type:

clib.mylib.x_myclass

To rename x_myclass, in the library definition file, change the name x_myclass to myClass, then build the interface. When you use the class in MATLAB, type:

clib.mylib.myClass

Use Invalid Property Names

You might need to access a property in MATLAB, but the name of the property might not be a valid MATLAB name. For example, the name might begin with an underscore. To derive this name at run time, use this MATLAB syntax, where propertyName is a string scalar or character vector that, when evaluated, returns an instance of a property.

clib.libName.className.(propertyName)

For example, suppose that you have interface clib.demo.MyClass with this property:

class MyClass
{
public:
    int _mean;
};

To assign property _mean to a variable, type:

x = clib.demo.MyClass;
xmean = x.('_mean')

This syntax is valid for names less than the maximum identifier length namelengthmax.

Use Invalid Enumerated Value Names

You might need to create an enumerated value in MATLAB, but the name of that value might not be a valid MATLAB name. For example, the enumerant name might begin with an underscore. To derive a value from this name at run time, use this MATLAB syntax, where enumMember is a string scalar or character vector that, when evaluated, returns an instance of an enumeration.

clib.libName.enumName.(enumMember)

For example, suppose that you have interface clib.enums.keywords with these properties:

  EnumDefinition with properties:

        Description: "clib.enums.keywords    Representation of C++ enumeration"
    DefiningLibrary: [1×1 clibgen.LibraryDefinition]
            CPPName: "keywords"
         MATLABType: "int32"
              Valid: 1
         MATLABName: "clib.enums.keywords"
            Entries: ["_for"    "_while"    "_class"    "_enums"    "_template"    "_typename"]

To assign entry _class to a variable, type:

var = clib.enums.keywords.('_class');

This syntax is valid for names less than the maximum identifier length namelengthmax.

Certain Class Names with typedef Aliases Not Supported

MATLAB does not support a class typedef alias name with the same name as a method in the class.