Format Generated Code Files by Using Templates
This example shows how to use a code generation template (.cgt
file) to add custom code banners, rearrange data and functions, and insert additional code segments and documentation into generated code files. Code generation templates can help you to meet your organization's coding standards by applying a consistent layout and appearance to the generated code.
Embedded Coder® provides a default, extensive code template file here:
matlabroot/toolbox/rtw/targets/ecoder/ert_code_template.cgt
Base your custom templates on this file and store them on the MATLAB® path. In the Model Configuration Parameters dialog box, specify them on the Code Generation > Templates pane. When you generate code, the code generator imports them.
Specify a Code Generation Template
The CodeTemplate
model uses a code generation template that extracts text from the Model Description tab of the Model Properties dialog box, a DocBlock block, and a Simulink® annotation. The Simulink annotation maps to the code template %<Note> symbol.
1. Open model CodeTemplate
.
model='CodeTemplate';
open_system(model)
2. In the Configuration Parameters dialog box, open Code Generation > Templates pane.
3. In the Code templates section, click the Edit button beside Source file template. The rtwdemocodetemplate.cgt
template opens in the MATLAB Command Window. Inspect the code template.
4. Build the model and generate code. On the C Code tab, click Build.
5. Open CodeTemplate.c
to inspect the generated code.
Modify a Code Generation Template
In this section, you will modify the rtwdemocodetemplate.cgt
file to remove the date and time. If you use a continuous integration system, removing this information might be helpful as checking files into source control can be more efficient without minor comment changes.
1. In the CodeTemplate.c
file, in the File Information section, observe the date and time.
2. Open the rtwdemocodetemplate.cgt
file and remove this line:
C source code generated on : %<SourceGeneratedOn>
3. Save the rtwdemocodetemplate.cgt
file.
4. Generate code.
5. Open the CodeTemplate.c
file and observe that the date and time are no longer there.
6. Close the model.
close_system(model)