• hmxmghl Created Project

    07/02/2016 | 7:49am

Loading Items

About This Project

Hi fellows. This project will instruct you about conversion of MATLAB/Simulink script to C/C++ code. As, time comes when we need our MATLAB algortihm to run on hardware to perform specfic tak. At that time, we can not directly run our MATLAB script on hardware. We need to convert our script to C/C++ code because most hardwares support this language. For example, you have developed a perfect noise cancellation algortihm in MATLAB and you need to implement it on DSK instrument to cancel the real time noise in speech. So, you need to convert your MATLAB script to C code in order to implement it on DSK instrument. There are many other times when it is essential for you to perform this conversion. In this project, I will instruct you about this conversion using MATLAB coder. Have a good time learning.

Request Access

Project Info

Skills

  • C
  • C++
  • Matlab
  • Overall

Meet The Makers

Join Team
  • hmxmghl
    • User:hmxmghl
    • Rank:Journeyman
    • Projects:10
    • Joined:2016

No Comments yet. Why don't you add one?

This project has two parts. Part-1 is about MATLAB script to C/C++ code conversion and Part-2 is about generating C code from Simulink block diagram. Both are very much similar. We will discuss Part-1 in detail and at the end we will skim the Part-2. Let’s begin.

.PART-1 (MATLAB to C/C++ code)

Step-1: (Opening/Creating .m file)

Generate the script file. Write the program in MATLAB or choose an existing program that you want to convert in C/C++ code. In this project I am going to convert 'correlation of two signals" algorithm into C code.

Step-2: (Converting algorithm in form of MATLAB function)

Converting this code to MATLAB function so that we can generate code of it. We will generate code from Coder. Coder is a software developed by MATH Works for converting MATLAB code to C/C++ code. As Coder cannot convert code of all MATLAB predefined functions into C/C++ language so we will eliminate the commands that coder does not support. First, we will make function of this code.

You can make MATLAB function as shown in figure below. Just pass the input variable(s) as argument to a function and return some variable(s) that you are storing your final result into. Here's a screenshot of MATLAB function of our code:

Step 3: (Including 'Codegen' Command)

Include the line %#codegen this will enables additional code generation checks. Below picture points out where to add this command:

As you can see on the left bar there is no warning sign so we can move to next step.

Step 4: (Using Coder to Generate Code)

Open the coder by typing coder in the command window. A window like this will appear:

In ‘generate code for function’ text box, enter your file name (better if you enter name with extension) and press the ‘next’ button at right bottom. When you will click on the next button then a window like this will appear:

Again write your filename and click on the “Let me enter input or global type directly”. This should be done because in MATLAB we do not need to define datatypes but C/C++ languages require datatype definition.

Now, select the data type of the variables in my case I am selecting double type. Image shows how to select datatypes:

Now, select the size of the matrix in my case I am selecting 1*2 and 1*2. This should be done becuase when variable is defined, its dimensions should be defined along. Below image shows how to select dimensions:

It will give you some issue check those issues. You can skip this step but if these issues are present in code Coder will not generate accurate C/C++ code.

Call your filaname and check fr issues. If there are not any major issues, like, usage o builtin MATLAB functions, then you can proceed. You can see in picture below that in my case first error is input command. As Coder does not convert all MATLAB functions to C code so this method is not suitable for this code conversion. In this I am removing the input command and reading the values from the functions input arguments like this.

Now my third error is stem command. Stem is a predefined method in MATLAB so coder will not generate C/C++ code so it is not suitable we will remove this command we can simply use printf in C language or C out in C++ for viewing output.

After removing the issues press the next button if you will further check the issues it will give you some warnings you can ignore these warnings and you can click the next button.

When you will click on next button you will have window like below. Click on generate button. By default it will generate the C code you can change the settings and generate the C++ code.

Now you can have generated code in C/C++ from MATLAB script file as shown below:

Click 'next' button and it will show you path to folder in which C/C++ code is generated.

.PART-2 (Simulink to C/C++ code)

Step 1:

Open the Simulink and generate the model that you want to convert in C/C++ code. In my case, I am taking this simple modulation Simulink model that is shown below:

Step 2:

Click on the simulation and then go to model configuration parameters set the solver parameters.

In my case, I am setting type to fixed step and solver to discrete (no continues states). Click the ok button and save the changes.

Now press the code generation button on the top right corner as shown in picture below. This will build the code folder in your folder with the name ‘filename_grt_rtw’.

You will get the C code after clicking on this button. Now, if you want to generate the code in C++ just go to simulation and then go to model configuration parameters go to code generation in code generation select the language as C++ and then press ok and do this step from the top.

You will get the generated C/C++ code in your current workspace folder. Generated code is shown below:

Note: Your current workspace folder should not be in the folder where MATLAB is installed.

So, thats how you can generate C/C++ code form MATLAB script and Simulink Block Diagram. I hope you learned what you were trying to. Thank you for viewing!

.Simulink to C code (Video Demonstartion):

https://youtu.be/Of5IhPysz1o

.MATLAB to C code (Video Demonstration):

https://youtu.be/47N9AkSiZqQ

  Details Downloads Size Get it

simulinkToC_allFiles

Last updated on 07/10/2016

by hmxmghl

526 134.3kb

matlabToC_allFiles

Last updated on 07/10/2016

by hmxmghl

436 124.6kb