Generating Object-Oriented C++ Code from MATLAB Algorithms
Learn how you can use MATLAB Coder™ to generate object-oriented C++ code from MATLAB® classes.
- Generate C++ Classes from MATLAB Classes: MATLAB Coder produces C++ classes from classes in your MATLAB code, including value classes, handle classes, and system objects. The generated code can be compiled into C++ libraries or executables and can be integrated into your existing C++ source code.
- Generate C++ Code with Namespaces: MATLAB Coder can generate C++ code in a namespace, making it easy to integrate with other source code that might have identical function or data type names. The code generator packages all generated functions and type definitions into the namespace.
- Use Dynamically Allocated C++ Arrays in Generated Function Interfaces: Generate C++ code for MATLAB functions that accept or return an array with an array size unknown at compile time, or whose bound exceeds a predefined threshold. In the generated code, memory for the array is dynamically allocated and implemented as a class template named coder::array. In addition to exception-safe memory deallocation, coder::array provides APIs to access and manage the dynamic array.
In this demonstration, I'm going to use MATLAB Coder to generate production C++ code from MATLAB algorithms. We've made a number of improvements to our C++ code generation and I want to highlight these in this video.
The MATLAB example I'm going to use represents oscillating systems. <Show a picture of Live Editor with the system's output>
Open app and setup
<Open App and select entry point in the background>
I'm going to generate some C++ classes from these MATLAB classes using the MATLAB Coder app. Everything I'll show today is also available from the command line using the codegen command.
The entry point is effectOfDamping. I need to tell MATLAB Coder the types of inputs the function needs. We can run the test script runEffectOfDamping to automatically detect my types.
Check for issues
I'll also use the same test script to check for run-time issues prior to generating my C++ code. <Check for issues in the background>
I'm going to generate a DLL and C++ to share this code with my team. Choosing C++ shows an option added in R2019b called "Interface style" where you can choose an interface with free functions or an interface using a class and methods on that class. I'm going to choose "methods" to get more idiomatic C++ and I'll name the class OscillatorInterface so we can find it in the generated code. <Generate code now>
Looking at generated code
To explore the generated code, let's open the MATLAB Coder Report. On the left, we see our input MATLAB code and the generated C++ code.
Let's take a look at our interface class resulting from that "interface style" option. The class is exported from the DLL, handles initialization and cleanup, and has our entry-point function as a public method. Any MATLAB persistent and global variables are mapped to class properties to make the generated code multi-instance.
MATLAB classes to C++ classes
Starting in R2020a MATLAB classes are mapped to C++ classes. R2020b adds the ability to map MATLAB packages to C++ namespaces. The generated C++ class mimics as many of the MATLAB class's attributes as possible.
In R2020a the generated code uses the coder::array class to manage dynamically allocated arrays. coder::array has an interface similar std::vector, provides indexing operator overloads, and ensures exception-safe memory deallocation.
SIL and debugging
Let's verify the behavior of our code. With Embedded Coder, <Select the test script and enable debugging> I can use SIL debugging to open the generated code in a debugger. <Run in the background> The code starts up in a separate process and that process is opened in Visual Studio for debugging. MATLAB Coder passes data from the script to the generated code.
Once we're happy with the behavior of the generated code, I'll package the code for my team in a ZIP file.
Later, if you want to transition from the App to a command line workflow for automated builds, choose the "Convert to script" option to get a script version of your project.
We've been continually improving MATLAB Coder. Since R20xx, we've added support for numerous MATLAB capabilities, including Table, DateTime, Timetable, and support for machine learning and deep learning. We strive to generate fast code that approaches or even exceeds hand code.
If you're interested in C++ code generation from MATLAB code I suggest taking a look at the new C++ capabilities in MATLAB Coder as of R2020b. Taken together, they allow for the generation of idiomatic and exception-safe C++ code using MATLAB Coder. The example code used in this video is available in the product documentation. Thanks for watching!
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- United Kingdom (English)
- Australia (English)
- India (English)
- New Zealand (English)
- 日本Japanese (日本語)
- 한국Korean (한국어)