It is possible to use C++ objects and methods inside the MATLAB code and generate code using the MATLAB Coder. The basic idea is to write a set of C++ functions that act as wrappers for the class methods and call these wrapper functions from within MATLAB Coder.
This workflow can be divided into 3 sections:
Part 1 - C++ Source Files:
- Define a void pointer that will be cast to the object type that will be used in a MATLAB function. This void pointer will be cast to the object type in each wrapper function.
- Create wrapper functions for class constructors and destructors. The argument to the constructor and destructor wrapper functions will be a void pointer that will be cast into the object type inside the wrapper functions.
- Inside the constructor wrapper function, allocate memory on the heap.
- Inside the destructor wrapper function, use "delete" function to free memory.
- For each class method that will be used in a MATLAB function, create a wrapper function. One of the arguments for these wrapper functions will be a void pointer.
- Inside the method wrapper functions, cast void pointer to the object type.
- Invoke the class methods inside the wrapper functions using the arrow operator (e.g. -> operator).
Part 2 - C++ Header Files:
- Create a header file for the wrapper functions and encapsulate the header file body with the "extern 'C' " statement in the following way:
#if defined __cplusplus
extern "C" {
#endif
//wrapper header body goes here.
#if defined __cplusplus
}
#endif
This is crucial so that the code can be compiled using a C++ Compiler and name mangling does not occur.
Part 3 - MATLAB Files:
- Use "coder.opaque" to declare the void pointer type in MATLAB functions. Note that MATLAB code cannot set or access a variable declared using "coder.opaque", but external C/C++ functions can accept it as an argument. Moreover, a variable declared using "coder.opaque" cannot be the output of an entry point MATLAB function.
- Use "coder.eval" to call the wrapper functions defined in Part 1 from withing MATLAB code.
The above workflow is mandatory to be able to call methods of a C++ class in MATLAB code. In C++, a class method is invoked using the dot operator. For example, to invoke the method "mymethod" of the class "myclass", you would use the following command:
"myclass.mymethod" is not a valid argument for "coder.ceval" in R2015b and earlier releases. Thus, the workaround is to write wrapper functions as explained in the above workflow. Please refer to the MATLAB Coder documentation and release notes for changes in future releases.
Please look at the attached zip file for a simple example of this workflow for a C++ "person" class. The source file is "person.cpp", and the header file is "person.h". The source file for the wrapper functions is "personwrapper.cpp" and the header file is "personwrapper.h". The MATLAB function that uses the wrapper functions is "persontest.m". The MAT-file "configmexexe" loads 2 Configuration objects to the base workspace; one is used for MEX file generation, and the other one is used for standalone executable generation. The "main.cpp" file is used for the executable. Please refer to the MATLAB script "generatecode.m" for generating a MEX file and an executable.