Saturday, November 20, 2021

How to convert Matlab functions into c or c++ code

In this tutorial, we are going to learn how we can convert the Matlab function into the c or c++ code. Note that the existing Matlab function needs to have c/c++ code generation extended capabilities.

Let's look into the sample Matlab scripts.

codegen.m

function y = codegen(I1)
 %#codegen
 y = typecast(I1, 'double');
Here, we are going to generate c code for function typecast of Matlab as given here. You can see the function does have the c/c++ code generation capabilities.

The first thing to do is add %#codegen as shown above. Open the file script on the editor and go to the APPS tab on the Matlab editor and you can find the MATLAB Coder option. Click on it.



Once you click on the MATLAB Coder option you can see the screen as below:



Select the script file created i.e codegen.m and the below screen will pop up and click the Next button for further steps.



Now, click on the link as shown below to add the input i.e I1 in our case which is defined in the function.



Now, lets define the input data.




Once click on the next button you can see the screen to check the issue, so you either check the issue or remove it by clicking on the screen outside of it. And click next to generate the code.
Click on generate button to generate the code. After build success, you can see the generated code as below:



This is a sample example for code generation. You can use the same methodologies for other Matlab inbuild functions to generate code in c/c++.
Share:

0 comments: