Generate C++ Classes from MATLAB Classes Using MATLAB Coder
Learn how to generate C++ classes from MATLAB® classes. Write MATLAB classes with public and private properties and methods, and MATLAB Coder™ will generate C++ classes with the equivalent private and public properties and methods. MATLAB classes such as value classes, handle classes, and system objects are supported.
Take your MATLAB classes, define with properties and methods and compile it into C++ classes with the equivalent properties and methods. So here instead of MATLAB, I have a simpleClass. And it's derived from the MATLAB handles class.
This particular class has two inputs, x and y. These are two points in space. And it has one output, z. Down here you will see that this is the initial constructor. And from here you have your set input, which is entering the values of x and y.
You have a get results method which is computing the output. And in the compute method, it's taking the two coordinates x and y and it's computing the distance between them. Once you compute it, you can use the get size x method to get the result.
So here's my actual algorithm. And instead of that, I've instantiated my simpleClass. Then I set the inputs. And from there, I ask it to compute. And finally, I'll get the result of the distance between my two inputs.
In addition, I have a simple test bench that exercises myAlg. And you can see with these two points A and B, the distance between these two points in space is the square root of 2. Since it runs in MATLAB, I can go ahead and generate code. So I'll start the MATLAB coder app.
Inside the MATLAB coder app, the first thing I need to do is to specify the following that I need to generate code for. So that's myAlg in this particular case. And the Code Readiness tool identified an issue that we need to take a look at before we start generating code.
So clicking on this, this is telling us that the try/catch block is not supported for co-generation. So in order to address it, we'll comment out try/catch. The Code Generation Readiness tool says everything's ready to go.
So next I need to define the input data types and sizes. The simple way is to exercise the test bench and asked MATLAB Coder to auto define the input types. So that's all set. Next, it's always a good idea to check for runtime issues. And in this step, MATLAB Coder will generate some trial code, build it, and see if there's any issues. In which case, we don't have any in this particular case.
So now we're ready generate code. We can select on C++ to generate code for our MATLAB class. OK, so we're done. We generated code successfully. Let's take a look at the code generation report. We go ahead and click on my myAlg.cpp.
We can take a look. And the resulting code here is instantiating my simpleClass, initializing it, setting the inputs, running compute method, and then finally returning the get result method. And it mirrors what we see in our MATLAB code pretty closely.
And the actual definition of simple class, you can see the compute method. And it's getting the 2 x and y-coordinates. And then it's computing the distance between them using square root.
Here you can see the get result method, which is just returning the private z variable back. And that was the initialization. Here in the set input method, you can see we're setting the coordinates of x and y. So that was a quick look at taking a MATLAB class and generating the equivalent C++ class with properties and methods using MATLAB coder.
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 (한국어)