Generating C Code from MATLAB Code
Generate readable and portable C and C++ code from MATLAB® code, including over 1,200 functions used for applications ranging from image processing and computer vision to advanced DSP and communications systems development, using MATLAB Coder™.
This video shows the workflow of generating C code through the use of a Kalman filter designed in MATLAB.
MATLAB Coder generates readable and portable C code from your MATLAB algorithms. This automated approach speeds up your design workflow and eliminates coding errors introduced by a manual translation process.
MATLAB Coder supports most of the MATLAB language and over 1,200 functions from a wide range of toolboxes used for applications ranging from image processing and computer vision to advanced DSP and communications systems development. To illustrate the workflow, I'm going to use a Kalman filter as an example numerical algorithm.
The Kalman filter DOTM file that you see here is slightly modified from the version that ships with MATLAB Coder. In addition, I have a simple test bench which gets the input, runs the Kalman filter, and plots it on the graph. While I have two files, I'm only interested in generating code for the algorithm.
Coming back to the algorithm, notice that I'm pre-allocating matrices like A and H. This is one of the tricks that helps generate better code. To start off, make sure you have the percentage pound cogent pragma at the top of your file. The pragma enables additional co-generation checks in the code analyzer, which is a tool built into the MATLAB editor.
Taking a look at the code analyzer at the top right corner, we see that it's flagged a couple of things. The first thing is that we need to complete statements with a semicolon. So let's fix that.
And over here with the brackets it's unnecessary. So we can delete it. Thirdly, MATLAB Coder does not currently support try-catch constructs. So let's remove it for now.
After fixing these things, the code analyzer is green so we're good to go. From here let's go and start the MATLAB Coder app. Here it is, under the co-generation category.
Here's the MATLAB Coder app. And the first thing we're going to do is to tell it which file we want to generate code for. From here, the code generation readiness tool makes an initial assessment and flags things like unsupported functions. If you have no issues, you actually won't see this screen. But in this case the tool is telling us that import data is not supported for co-generation.
We wrote this chunk of code for debugging purposes as we were designing the algorithm. And since we don't need to generate code for this diagnostic code, we can comment it out and move to the next step. Now we need to define our input types. When working with C code we need to explicitly specify the data type and sizes of our inputs.
The easiest way to do this is to use a test bench that exercises the function that we want to generate code for. In this case this is the Kalman filter test bench that I showed you earlier. And if we click on the outer defined input types, MATLAB Coder will run the test bench and infer the input data types and sizes.
It figures out that our input data type is a two-by-one array of type double. So we'll accept that. If you want to, you can also explicitly set the data type and sizes yourself. Moving to the next step, MATLAB Coder is going to check for runtime errors in the generated code.
They'll perform things like array bounds checking and dimension checking to look for violations of memory integrity. In this case, everything passes with flying colors. So we can move on.
Now we're ready to generate code. For our output we can generate source code, MEX files, static or dynamic libraries, or an executable. To keep things simple let's go ahead and generate source code. So we just generate code successfully.
I enabled the option to insert the MATLAB code as comments in the generated C code so that it's easier to trace back. We also have a header file here. At this point there are a couple of things you can do. You can of course create a MEX file and bring it back to MATLAB for acceleration purposes. You can tune some of the generation options related to speed, memory usage, and code appearance and then regenerate code. You can also do some code verification.
Of course, you can simply export the generated code and use it elsewhere. This was a quick overview of generating C code from your MATLAB code. Take a look at our documentation for more examples and other details.
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.