Main Content

Format Generated Code Files by Using Templates

This example shows how to use code generation templates to add custom code banners, rearrange data and functions, and insert additional code segments and documentation into generated code files.

The model extracts text from the Model Description, a DOC block, and a Simulink® annotation. The Simulink annotation maps to the code template %<Note> symbol.

The templates provide consistent file layout and elaboration for your organization's coding standards. Embedded Coder® provides extensible code generation templates for formatting generated code. The layout and format of the code template file controls the output of the generated code.

Code generation templates (.cgt files) enable you to add custom code banners, rearrange data and functions, and insert additional code segments and documentation into the generated code files. Code generation templates are imported automatically from the MATLAB® path. For this model, the code generation template rtwdemocodetemplate.cgt is used.

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.

6. Close the model.

close_system(model)