使用模板格式化生成的代码文件
此示例说明如何使用代码生成模板(.cgt 文件)添加自定义代码前注,重新排列数据和函数,并在生成的代码文件中插入或删除其他代码段和文档。通过对生成的代码应用一致的布局和外观,代码生成模板可以帮助您满足组织的编码标准。
Embedded Coder® 在以下位置提供默认代码模板文件:
matlabroot/toolbox/rtw/targets/ecoder/ert_code_template.cgt
基于此文件自定义您的模板,并将它们存储在 MATLAB® 路径中。在“模型配置参数”对话框中,在代码生成 > 模板窗格中指定它们。当您生成代码时,代码生成器会导入您的模板。
使用默认代码生成模板
打开示例模型 CodeTemplate。
model='CodeTemplate';
open_system(model)
验证模型是否使用默认代码生成模板。
在“配置参数”对话框中,打开代码生成 > 模板窗格。
在代码模板部分,验证源文件模板字段是否设置为
ert_code_template.cgt。点击源文件模板旁边的编辑按钮。
ert_code_template.cgt模板在 MATLAB 命令行窗口中打开。检查代码模板。

生成代码。在 C 代码选项卡上,点击编译。
evalc('slbuild(model)');打开 CodeTemplate.c 以检查生成的代码。
file = fullfile("CodeTemplate_ert_rtw","CodeTemplate.c"); coder.example.extractLines(file,"File: CodeTemplate.c","*/",1,1)
* File: CodeTemplate.c * * Code generated for Simulink model 'CodeTemplate'. * * Model version : 14.0 * Simulink Coder version : 26.1 (R2026a) 20-Nov-2025 * C/C++ source code generated on : Sun Jan 25 21:00:46 2026 * * Target selection: ert.tlc * Embedded hardware selection: Intel->x86-64 (Windows64) * Code generation objectives: Unspecified * Validation result: Not run */
指定代码生成模板
指定源文件模板 rtwdemocodetemplate.cgt。此模板从 DocBlock 模块 Abstract 和“模型属性”对话框的模型描述选项卡中提取文本。
在代码模板部分的源文件模板字段中,输入 rtwdemocodetemplate.cgt。
set_param(model,ERTSrcFileBannerTemplate="rtwdemocodetemplate.cgt");点击源文件模板旁边的编辑按钮。rtwdemocodetemplate.cgt 模板在 MATLAB 命令行窗口中打开。检查代码模板。
生成代码。在 C 代码选项卡上,点击编译。
evalc('slbuild(model)');打开 CodeTemplate.c 文件以检查生成的代码。Abstract 模块和“模型属性”对话框的文本分别显示在 ABSTRACT 和 MODEL INFORMATION 标题下的自定义文件前注中。
file = fullfile("CodeTemplate_ert_rtw","CodeTemplate.c"); coder.example.extractLines(file,"FILE INFORMATION:","additional code segments",1,1)
* FILE INFORMATION: * File name: CodeTemplate.c * C source code generated on : Sun Jan 25 21:00:52 2026 * * ABSTRACT: * This text maps to the code template %<Abstract> symbol. * * NOTES: * * * MODEL INFORMATION: * Model Name: CodeTemplate * Model Version: 14.0 * Model Description: Formatting Generated Files with 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.
修改代码生成模板
生成代码中的自定义文件前注显示代码生成的时间戳。如果您使用持续集成系统,删除此信息可能会有所帮助,因为将文件签入源代码管理可以更高效,无需进行细微注释修改。
修改源文件模板以删除时间戳。打开 rtwdemocodetemplate.cgt 文件并删除以下行。
C source code generated on : %<SourceGeneratedOn>
!sed -i '15d' rtwdemocodetemplate.cgt保存 rtwdemocodetemplate.cgt 文件。
删除模型缓存文件和 slprj 工程目录。然后重新生成代码。
delete *.slxc; rmdir slprj s; evalc('slbuild(model)');
打开 CodeTemplate.c 文件,观察时间戳不再存在。
coder.example.extractLines(file,"FILE INFORMATION:","additional code segments",1,1)
* FILE INFORMATION: * File name: CodeTemplate.c * * ABSTRACT: * This text maps to the code template %<Abstract> symbol. * * NOTES: * * * MODEL INFORMATION: * Model Name: CodeTemplate * Model Version: 14.0 * Model Description: Formatting Generated Files with 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.