0
0
Simulinkdata~5 mins

Generated C code inspection in Simulink

Choose your learning style9 modes available
Introduction

Generated C code inspection helps you check the code created automatically from Simulink models. It ensures the code works as expected and is easy to understand.

You want to verify that the automatic code matches your Simulink model logic.
You need to find errors or inefficiencies in the generated C code.
You want to learn how Simulink converts models into C code for embedded systems.
You need to document or explain the generated code to your team.
You want to optimize or customize the generated code for better performance.
Syntax
Simulink
1. Open your Simulink model.
2. Configure code generation settings.
3. Generate C code using Simulink Coder.
4. Use the Code Inspector tool to review the generated code.
5. Navigate through code files and compare with model elements.

The Code Inspector is a graphical tool inside Simulink for easy code review.

You can link code sections back to model blocks for better understanding.

Examples
This command generates C code from the Simulink model named 'my_model'.
Simulink
simulink_model = 'my_model';
rtwbuild(simulink_model);
This sequence opens the model, sets it to generate code only (no build), then generates the code.
Simulink
open_system('my_model');
set_param('my_model', 'GenerateCodeOnly', 'on');
rtwbuild('my_model');
This command opens the Code Inspector for the generated code folder 'my_model_ert_rtw'.
Simulink
codeInspector('my_model_ert_rtw');
Sample Program

This example opens a Simulink model called 'simple_model', sets it to generate embedded C code only, builds the code, then opens the Code Inspector to review the generated files.

Simulink
model = 'simple_model';
open_system(model);
set_param(model, 'SystemTargetFile', 'ert.tlc');
set_param(model, 'GenerateCodeOnly', 'on');
rtwbuild(model);
codeFolder = [pwd filesep model '_ert_rtw'];
codeInspector(codeFolder);
OutputSuccess
Important Notes

Always check that your model is error-free before generating code.

Use the Code Inspector to trace code back to model blocks for easier debugging.

Generated code may be complex; focus on key functions and variables related to your model.

Summary

Generated C code inspection helps verify and understand code from Simulink models.

Use Simulink Coder and Code Inspector tools to generate and review code.

Inspecting code ensures correctness and helps optimize embedded system software.