MATLAB is a high-level, interpretive programming
language that provides users with out-of-the-box functions
for specialized workflows in a variety of areas,
like data science, image processing, control systems--
to name a few.
Programming in high-level language
is great while designing or prototyping algorithms,
as you can try out different permutations and combinations
quickly in a few lines of code, without having to wait
for the code to compile.
The trade off with an interpreted language
is often with code performance.
When moving to a production environment,
you might want to use a low-level language for better
performance.
This is where code generation comes in.
You can generate standalone, readable and editable,
low-level C/C++ code, directly from MATLAB,
to reduce execution time, deploy to embedded systems,
or integrate with other code projects.
So let's see how we can do this.
To show you how to generate code from MATLAB,
I will use a simple image processing example.
This example resizes an input image by half,
and changes the color space from RGB to grayscale.
Note that even though this is an image processing example,
the same process can be followed, irrespective
of the application.
The first step to generate code is
to convert the algorithmic portion of your code
into a function.
Let's copy over the resize and color space conversion
function calls into a new function,
and call it resizeAndGray, and replace these calls
with the new function in the test script,
resizeAndGray_test.
You can now use the CodeGen directive
to do an initial check, to see if your function is
CodeGen compatible or not.
The green signal in the right hand corner
indicates that the function is ready for code generation.
Next, open the MATLAB coded app from the Apps tab.
The first screen in the app requires an entry point
function.
The entry point function is the function containing
the algorithm that will get converted into C/C++ code.
Click Next.
The next screen is all about defining input types.
You can manually define input types for each input
to the function, or have MATLAB auto-define them for you.
Enter code that calls the function,
or pick a test script that calls the entry point function,
and MATLAB will automatically identify the number of inputs
their data type and size.
Click Next.
The third step is to check for runtime issues.
In this step, the app will generate a MATLAB executable,
or a MEX function, and replace calls
to the entry point function with the generated MEX function.
It will then run the test script so you
can visualize that the code indeed performs
as expected in code generation.
You can review the TargetBuild log
for any issues with the MEX function generation.
Click Next.
The final step is to generate C/C++ code.
Specify the build type as appropriate.
We will choose the source code option,
but notice that you can also generate
static or dynamic libraries, et cetera.
Choose the targets language as appropriate-- in our case C++--
and the hardware board as the MATLAB host computer.
With the help of support packages,
you can choose a variety of different target
hardware for your application.
Now click the Generate button.
Once the code is generated, you will be presented
with a code generation report.
You can use this to examine the generated code
and package the code.
The Trace Code functionality lets you visualize the C/C++
code that corresponds to a particular line of MATLAB code.
The MATLAB coded app can also generate
a script of all the actions that you have just taken in the app
to generate the code.
This is useful if you need to repeat
this process multiple times.
Notice that it is a call to the CodeGen function
that actually generates the code.
To conclude, can use the MATLAB coded app to generate low-level
C/C++ code, from a MATLAB function, that is readable,
editable, and can be used outside of MATLAB.
If you want to gain a more in-depth understanding of code
generation, and different code customizations
and optimizations, please check out the code generation
tutorial series linked in the resources below.