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
andset
methods for public properties of MATLAB classes. The property names of MATLAB classes are prepended withget
orset
. For example, if the property name in a MATLAB class isUpperLeft
, the corresponding C++ method names aregetUpperLeft
andsetUpperLeft
.
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 Class | Snippet 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 |
|
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 thematlab.engine.typedinterface.generateCPP
function and theMatlabEngine.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 fromarguments
andproperties
blocks. The retrieved information is array size, type, and whether it is a real number.
See Also
matlab.engine.typedinterface.generateCPP
| arguments
| properties