| Real-Time Workshop® Embedded Coder™ | ![]() |
This section describes Real-Time Workshop® Embedded Coder™ custom file processing (CFP) features. Custom file processing simplifies generation of custom source code by letting you
Generate virtually any type of source (.c or .cpp) or header (.h) file. Using a custom file processing template (CFP template), you can control how code is emitted to the standard generated model files (for example, model.c or .cpp, model.h) or generate files that are independent of model code.
Organize generated code into sections (such as includes, typedefs, functions, and more). Your CFP template can emit code (for example, functions), directives (such as #define or #include statements), or comments into each section as required.
Generate custom file banners (comment sections) at the start and end of generated code files.
Generate code to call model functions such as model_initialize, model_step, and so on.
Generate code to read and write model inputs and outputs.
Generate a main program module.
Obtain information about the model and the files being generated from it.
The custom file processing features discussed in this section are based on the following interrelated components:
Code generation template (CGT) files: A CGT file defines the top-level organization and formatting of generated code. CGT files are described in Code Generation Template (CGT) Files.
The code template API: a high-level Target Language Compiler (TLC) API that provides functions that let you organize code into named sections and subsections of generated source and header files. The code template API also provides utilities that return information about generated files, generate standard model calls and perform other useful functions. See Code Template API Summary.
Custom file processing (CFP) templates: A CFP template is a TLC file that manages the process of custom code generation. The primary purpose of a CFP template is to assemble code to be generated into buffers, and to call the code template API to emit the buffered code into specified sections of generated source and header files. A CFP template interacts with a CGT file, which defines the ordering of major sections into which code is emitted. CFP templates and their applications are described in Using Custom File Processing (CFP) Templates.
Understanding of TLC programming is required to use CFP templates. See the Target Language Compiler document to learn the basics.
Use of custom file processing features requires creation of CGT files and/or CFP templates. Usually, these files are based on default templates provided by the Real-Time Workshop Embedded Coder software. Once you have created your templates, you must integrate them into the code generation process.
The Templates pane of the Real-Time Workshop properties of a model configuration set lets you select and edit CGT files and CFP templates, and specify their use in the code generation process. Real-Time Workshop/Templates Pane shows this pane, with all options configured for their defaults.
Real-Time Workshop/Templates Pane

The options related to custom file processing are:
The Source file (.c) template field in the Code templates and Data templates sections. This field specifies the name of a CGT file to use when generating source (.c or .cpp) files. This file must be located on the MATLAB® path.
The Header file (.h) template field in the Code templates and Data templates sections. This field specifies the name of a CGT file to use when generating header (.h) files. This file must be located on the MATLAB path.
By default, the template for both source and header files is matlabroot/toolbox/rtw/targets/ecoder/ert_code_template.cgt.
The File customization template edit field in the Custom templates section. This field specifies the name of a CFP template file to use when generating code files. This file must be located on the MATLAB path. The default CFP template is matlabroot/toolbox/rtw/targets/ecoder/example_file_process.tlc.
Each of these fields has associated Browse and Edit buttons. Browse lets you navigate to and select an existing CFP template or CGT file. Edit opens the specified CFP template into the MATLAB editor, where you can customize it.
CGT files have a number of applications:
The simplest application is generation of custom file banners (comments sections) in code files. To do this, no knowledge of the details of the CGT file structure is required; see Generating Custom File Banners.
Some of the advanced features described in the Module Packaging Features document utilize CGT files. Refer to that document for information.
When generating custom code using a CFP template, a CGT file is required. Correct use of CFP templates requires understanding of the CGT file structure, although in many cases it is possible to use the default CGT file without modification.
The Real-Time Workshop Embedded Coder software provides a default CGT file: matlabroot/toolbox/rtw/targets/ecoder/ert_code_template.cgt.
You should base your custom CGT files on the default file.
A CGT file consists of three sections:
Header Section. This section is optional. It contains comments and tokens for use in generating a custom header banner. Generating Custom File Banners gives details on custom banner generation.
Code Insertion Section. This section is required. It contains tokens that define an ordered partitioning of the generated code into a number of sections (such as Includes and Defines sections). Tokens have the form
%<SectionName>
For example,
%<Includes>
The Real-Time Workshop Embedded Coder software defines a minimal set of tokens that are required for the generation of C or C++ source or header code. These are built-in tokens (see Built-In Tokens and Sections). You can also define custom tokens and add them to the code insertion section (see Generating a Custom Section).
Each token functions as a placeholder for a corresponding section of generated code. The ordering of the tokens defines the order in which the corresponding sections appear on the generated code. The presence of a token in the CGT file does not guarantee that the corresponding section is generated. To generate code into a given section, you must do so explicitly by calling the code template API from a CFP template, as described in Using Custom File Processing (CFP) Templates.
The CGT tokens define the high-level organization of generated code. Using the code template API, you can partition each code section into named subsections, as described in Subsections.
You can also insert C or C++ comments into the code insertion section, between tokens. Such comments are inserted directly into the generated code.
Trailer Section. This section is optional. It contains comments and tokens for use in generating a custom trailer banner. Generating Custom File Banners gives details on custom banner generation.
The following code extract shows the code insertion section of the default CGT file, showing the built-in tokens.
%% Required tokens. You can insert comments and other tokens in between them, %% but do not change their order or remove them. %% %<Includes> %<Defines> %<Types> %<Enums> %<Definitions> %<Declarations> %<Functions>
Note carefully the following requirements before creating or customizing a CGT file:
All the built-in tokens are required. None can be removed.
Built-in tokens must appear in the order shown. The ordering is significant because each successive section can have dependencies on previous sections.
Only one token can appear per line.
Tokens must not be repeated.
Custom tokens can be added to the code insertion section, provided that the previous requirements are not violated.
Comments can be added to the code insertion section, provided that the previous requirements are not violated.
Built-In CGT Tokens and Corresponding Code Sections summarizes the built-in tokens and corresponding section names, and describes the code sections.
Built-In CGT Tokens and Corresponding Code Sections
Token / Section Name | Description |
|---|---|
Includes | #include directives section |
Defines | #define directives section |
Types | typedef section. Typedefs can depend on any previously defined type |
Enums | Enumerated types section |
Definitions | Place data definitions here (for example, double x = 3.0;) |
Declarations | Data declarations (for example, extern double x;) |
Functions | C or C++ functions |
It is possible to define one or more named subsections for any section. Some of the built-in sections have predefined subsections. These are summarized in Subsections Defined for Built-In Sections.
It is important to note that the sections and subsections listed in Subsections Defined for Built-In Sections are emitted, in the order listed, to the source or header file being generated.
The custom section feature lets you define sections in addition to those listed in Subsections Defined for Built-In Sections. See Generating a Custom Section for information on how to do this.
Subsections Defined for Built-In Sections
Section | Subsections | Subsection Description |
|---|---|---|
Includes | N/A |
|
Defines | N/A |
|
Types | IntrinsicTypes | Intrinsic typedef section. Intrinsic types are those that depend only on intrinsic C or C++ types. |
Types | PrimitiveTypedefs | Primitive typedef section. Primitive typedefs are those that depend only on intrinsic C or C++ types and on any typedefs previously defined in the IntrinsicTypes section. |
Types | UserTop | Any type of code can be placed in this section. You can place code that has dependencies on the previous sections here. |
Types | Typedefs | typedef section. Typedefs can depend on any previously defined type |
Enums | N/A |
|
Definitions | N/A |
|
Declarations | N/A |
|
Functions |
| C or C++ functions |
Functions | CompilerErrors | #warning directives |
Functions | CompilerWarnings | #error directives |
Functions | Documentation | Documentation (comment) section |
Functions | UserBottom | Any code can be placed in this section. |
The files provided to support custom file processing are
matlabroot/rtw/c/tlc/mw/codetemplatelib.tlc: A TLC function library that implements the code template API. codetemplatelib.tlc also provides the comprehensive documentation of the API in the comments headers preceding each function.
matlabroot/toolbox/rtw/targets/ecoder/example_file_process.tlc: An example custom file processing (CFP) template, which you should use as the starting point for creating your own CFP templates. Guidelines and examples for creating a CFP template are provided in Generating Source and Header Files with a Custom File Processing (CFP) Template.
TLC files supporting generation of single-rate and multirate main program modules (see Customizing Main Program Module Generation).
Once you have created a CFP template, you must integrate it into the code generation process, using the File customization template edit field (see Custom File Processing User Interface Options).
A custom file processing (CFP) template imposes a simple structure on the code generation process. The template, in conjunction with a code generation template (CGT) file, partitions the code generated for each file into a number of sections. These sections are summarized in Built-In CGT Tokens and Corresponding Code Sections and Subsections Defined for Built-In Sections.
Code for each section is assembled in buffers and then emitted, in the order listed, to the file being generated.
To generate a file section, your CFP template must first assemble the code to be generated into a buffer. Then, to emit the section, your template calls the TLC function
LibSetSourceFileSection(fileH, section, tmpBuf)
where
fileH is a file reference to a file being generated.
section is the code section or subsection to which code is to be emitted. section must be one of the section or subsection names listed in Subsections Defined for Built-In Sections.
Determine the section argument as follows:
If Subsections Defined for Built-In Sections defines no subsections for a given section, use the section name as the section argument.
If Subsections Defined for Built-In Sections defines one or more subsections for a given section, you can use either the section name or a subsection name as the section argument.
If you have defined a custom token denoting a custom section, do not call LibSetSourceFileSection. Special API calls are provided for custom sections (see Generating a Custom Section).
tmpBuf is the buffer containing the code to be emitted.
There is no requirement to generate all of the available sections. Your template need only generate the sections you require in a particular file.
Note that no legality or syntax checking is performed on the custom code within each section.
The next section, Generating Source and Header Files with a Custom File Processing (CFP) Template, provides typical usage examples.
This section walks you through the process of generating a simple source (.c or .cpp) and header (.h) file using the example CFP template. Then, it examines the template and the code generated by the template.
The example CFP template, matlabroot/toolbox/rtw/targets/ecoder/example_file_process.tlc, demonstrates some of the capabilities of the code template API, including
Generation of simple source (.c or .cpp) and header (.h) files
Use of buffers to generate file sections for includes, functions, and so on
Generation of includes, defines, and so on into the standard generated files (for example, model.h)
Generation of a main program module
This section sets up a CFP template and configures a model to use the template in code generation. The template generates (in addition to the standard model files) a source file (timestwo.c or .cpp) and a header file (timestwo.h).
You should follow the steps below to become acquainted with the use of CFP templates:
Copy the example CFP template, matlabroot/toolbox/rtw/targets/ecoder/example_file_process.tlc, to a directory of your choice. This directory should be located outside the MATLAB directory structure (that is, it should not be under matlabroot.) Note that this directory must be on the MATLAB path, or on the TLC path. It is good practice to locate the CFP template in the same directory as your system target file, which is guaranteed to be on the TLC path.
Rename the copied example_file_process.tlc to test_example_file_process.tlc.
%% %assign ERTCustomFileTest = TLC_TRUE
It should now read:
%assign ERTCustomFileTest = TLC_TRUE
If ERTCustomFileTest is not assigned as shown, the CFP template is ignored in code generation.
Save your changes to the file. Keep test_example_file_process.tlc open, so you can refer to it later.
Open the rtwdemo_udt model.
Open the Simulink® Model Explorer. Select the active configuration set of the model, and open the Real-Time Workshop properties view of the active configuration set.
Configure the File customization template field as shown below. The test_example_file_process.tlc file, which you previously edited, is now specified as the CFP template.

Select the Generate code only option.
Click Generate code. During code generation, notice the following message on the MATLAB command window:
Warning: Overriding example ert_main.c!
This message is displayed because test_example_file_process.tlc generates the main program module, overriding the default action of the ERT target. This is explained in greater detail below.
The rtwdemo_udt model is configured to generate an HTML code generation report. After code generation completes, view the report. Notice that the Generated Source Files list contains the files timestwo.c, timestwo.h, and ert_main.c. These files were generated by the CFP template. The next section examines the template to learn how this was done.
Keep the model, the code generation report, and the test_example_file_process.tlc file open so you can refer to them in the next section.
This section examines excerpts from test_example_file_process.tlc and some of the code it generates. You should refer to the comments in matlabroot/rtw/c/tlc/mw/codetemplatelib.tlc while reading the discussion below.
Generating Code Files. Source (.c or .cpp) and header (.h) files are created by calling LibCreateSourceFile, as in the following excerpts:
%assign cFile = LibCreateSourceFile("Source", "Custom", "timestwo")
...
%assign hFile = LibCreateSourceFile("Header", "Custom", "timestwo")Subsequent code refers to the files by the file reference returned from LibCreateSourceFile.
File Sections and Buffers. The code template API lets you partition the code generated to each file into sections, tagged as Definitions, Includes, Functions, Banner, and so on. You can append code to each section as many times as required. This technique gives you a great deal of flexibility in the formatting of your custom code files.
The available file sections, and the order in which they are emitted to the generated file, are summarized in Subsections Defined for Built-In Sections.
For each section of a generated file, use %openfile and %closefile to store the text for that section in temporary buffers. Then, to write (append) the buffer contents to a file section, call LibSetSourceFileSection, passing in the desired section tag and file reference. For example, the following code uses two buffers (tmwtypesBuf and tmpBuf) to generate two sections (tagged "Includes" and "Functions") of the source file timestwo.c or .cpp (referenced as cFile):
%openfile tmwtypesBuf
#include "tmwtypes.h"
%closefile tmwtypesBuf
%<LibSetSourceFileSection(cFile,"Includes",tmwtypesBuf)>
%openfile tmpBuf
/* Times two function */
real_T timestwofcn(real_T input) {
return (input * 2.0);
}
%closefile tmpBuf
%<LibSetSourceFileSection(cFile,"Functions",tmpBuf)>These two sections generate the entire timestwo.c or .cpp file:
#include "tmwtypes.h"
/* Times two function */
FLOAT64 timestwofcn(FLOAT64 input)
{
return (input * 2.0);
}Adding Code to Standard Generated Files. The timestwo.c or .cpp file generated in the previous example was independent of the standard code files generated from a model (for example, model.c or .cpp, model.h, and so on). You can use similar techniques to generate custom code within the model files. The code template API includes functions to obtain the names of the standard models files and other model-related information. The following excerpt calls LibGetMdlPubHdrBaseName to obtain the correct name for the model.h file. It then obtains a file reference and generates a definition in the Defines section of model.h:
%% Add a #define to the model's public header file model.h
%assign pubName = LibGetMdlPubHdrBaseName()
%assign modelH = LibCreateSourceFile("Header", "Simulink", pubName)
%openfile tmpBuf
#define ACCELERATION 9.81
%closefile tmpBuf
%<LibSetSourceFileSection(modelH,"Defines",tmpBuf)>Examine the generated rtwdemo_udt.h file to see the generated #define directive.
Customizing Main Program Module Generation. Normally, the ERT target determines whether and how to generate an ert_main.c or .cpp module based on the settings of the Generate an example main program and Target operating system options on the Templates pane of the Configuration Parameters dialog box. You can use a CFP template to override the normal behavior and generate a main program module customized for your target environment.
To support generation of main program modules, two TLC files are provided:
bareboard_srmain.tlc: TLC code to generate an example single-rate main program module for a bareboard target environment. Code is generated by a single TLC function, FcnSingleTaskingMain.
bareboard_mrmain.tlc: TLC code to generate a multirate main program module for a bareboard target environment. Code is generated by a single TLC function, FcnMultiTaskingMain.
In the example CFP template file matlabroot/toolbox/rtw/targets/ecoder/example_file_process.tlc, the following code generates either a single- or multitasking ert_main.c or .cpp module. The logic depends on information obtained from the code template API calls LibIsSingleRateModel and LibIsSingleTasking:
%% Create a simple main. Files are located in MATLAB/rtw/c/tlc/mw. %if LibIsSingleRateModel() || LibIsSingleTasking() %include "bareboard_srmain.tlc" %<FcnSingleTaskingMain()> %else %include "bareboard_mrmain.tlc" %<FcnMultiTaskingMain()> %endif
Note that bareboard_srmain.tlc and bareboard_mrmain.tlc use the code template API to generate ert_main.c or .cpp.
When generating your own main program module, you disable the default generation of ert_main.c or .cpp. The TLC variable GenerateSampleERTMain controls generation of ert_main.c or .cpp. You can directly force this variable to TLC_FALSE. The examples bareboard_mrmain.tlc and bareboard_srmain.tlc use this technique, as shown in the following excerpt from bareboard_srmain.tlc.
%if GenerateSampleERTMain
%assign CompiledModel.GenerateSampleERTMain = TLC_FALSE
%warning Overriding example ert_main.c!
%endifAlternatively, you can implement a SelectCallback function for your target. A SelectCallback function is an M function that is triggered during model loading, and also when the user selects a target with the System Target File browser. Your SelectCallback function should deselect and disable the Generate an example main program option. This prevents the TLC variable GenerateSampleERTMain from being set to TLC_TRUE.
See the rtwgensettings Structure section of the Developing Embedded Targets document for information on creating a SelectCallback function.
The following code illustrates how to deselect and disable the Generate an example main program option in the context of a SelectCallback function.
slConfigUISetVal(hDlg, hSrc, 'GenerateSampleERTMain', 'off'); slConfigUISetEnabled(hDlg, hSrc, 'GenerateSampleERTMain',0);
Note Creation of a main program for your target environment requires some customization; for example, in a bareboard environment you need to attach rt_OneStep to a timer interrupt. It is expected that you will customize either the generated code, the generating TLC code, or both. See Guidelines for Modifying the Main Program and Guidelines for Modifying rt_OneStep for further information. |
You can define custom tokens in a CGT file and direct generated code into an associated built-in section. This feature gives you additional control over the formatting of code within each built-in section. For example, you could add subsections to built-in sections that do not already define any subsections. All custom sections must be associated with one of the built-in sections: Includes, Defines, Types, Enums, Definitions, Declarations, or Functions. To create custom sections, you must
Add a custom token to the code insertion section of your CGT file.
In your CFP file:
Assemble code to be generated to the custom section into a buffer.
Declare an association between the custom section and a built-in section, with the code template API function LibAddSourceFileCustomSection.
Emit code to the custom section with the code template API function LibSetSourceFileCustomSection.
The following code examples illustrate the addition of a custom token, Myincludes, to a CGT file, and the subsequent association of the custom section Myincludes with the built-in section Includes in a CFP file.
Note If you have not already created custom CGT and CFP files for your model, copy the default template files matlabroot/toolbox/rtw/targets/ecoder/ert_code_template.cgt and matlabroot/toolbox/rtw/targets/ecoder/example_file_process.tlc to a work directory that is outside the MATLAB directory structure but on the MATLAB or TLC path, rename them (for example, add the prefix test_ to each file), and update the Templates pane of the Configuration Parameters dialog box to correctly reference them. |
First, add the token Myincludes to the code insertion section of your CGT file. For example:
%<Includes> %<Myincludes> %<Defines> %<Types> %<Enums> %<Definitions> %<Declarations> %<Functions>
Next, in the CFP file, add code to generate include directives into a buffer. For example, in your copy of the example CFP file, you could insert the following section between the Includes section and the Create a simple main section:
%% Add a custom section to the model's C file model.c %openfile tmpBuf #include "moretables1.h" #include "moretables2.h" %closefile tmpBuf %<LibAddSourceFileCustomSection(modelC,"Includes","Myincludes")> %<LibSetSourceFileCustomSection(modelC,"Myincludes",tmpBuf)>
The LibAddSourceFileCustomSection function call declares an association between the built-in section Includes and the custom section Myincludes. In effect, Myincludes is a subsection of Includes. The LibSetSourceFileCustomSection function call directs the code in the tmpBuf buffer to the Myincludes section of the generated file. LibSetSourceFileCustomSection is syntactically identical to LibSetSourceFileSection.
In the generated code, the include directives generated to the custom section appear after other code directed to Includes.
#include "rtwdemo_udt.h" #include "rtwdemo_udt_private.h" /* #include "mytables.h" */ #include "moretables1.h" #include "moretables2.h"
Note The placement of the custom token in this example CGT file is arbitrary. By locating %<Myincludes> after %<Includes>, the CGT file ensures only that the Myincludes code appears after Includes code. |
Code Template API Functions summarizes the code template API. See the source code in matlabroot/rtw/c/tlc/mw/codetemplatelib.tlc for detailed information on the arguments, return values, and operation of these calls.
Code Template API Functions
Function | Description |
|---|---|
LibGetNumSourceFiles | Returns the number of created source files (.c or .cpp and .h). |
LibGetSourceFileTag | Returns <filename>_h and <filename>_c for header and source files, respectively, where filename is the name of the model file. |
LibCreateSourceFile | Creates a new C or C++ file and returns its reference. If the file already exists, simply returns its reference. |
LibGetSourceFileFromIdx | Returns a model file reference based on its index. This is useful for a common operation on all files, such as to set the leading file banner of all files. |
LibSetSourceFileSection | Adds to the contents of a specified section within a specified file (see also Custom File Processing (CFP) Template Structure). |
LibIndentSourceFile | Indents a file with the Real-Time Workshop® c_indent utility (from within the TLC environment). |
LibCallModelInitialize | Returns code for calling the model's model_initialize function (valid for ERT only). |
LibCallModelStep | Returns code for calling the model's model_step function (valid for ERT only). |
LibCallModelTerminate | Returns code for calling the model's model_terminate function (valid for ERT only). |
LibCallSetEventForThisBaseStep | Returns code for calling the model's set events function (valid for ERT only). |
LibWriteModelData | Returns data for the model (valid for ERT only). |
LibSetRTModelErrorStatus | Returns the code to set the model error status. |
LibGetRTModelErrorStatus | Returns the code to get the model error status. |
LibIsSingleRateModel | Returns true if model is single rate and false otherwise. |
LibGetModelName | Returns name of the model (no extension). |
LibGetMdlSrcBaseName | Returns the name of model's main source file (for example, model.c or .cpp). |
LibGetMdlPubHdrBaseName | Returns the name of model's public header file (for example, model.h). |
LibGetMdlPrvHdrBaseName | Returns the name of the model's private header file (for example, model_private.h). |
LibIsSingleTasking | Returns true if the model is configured for singletasking execution. |
LibWriteModelInput | Returns the code to write to a particular root input (that is, a model inport block). (valid for ERT only). |
LibWriteModelOutput | Returns the code to write to a particular root output (that is, a model outport block). (valid for ERT only). |
LibWriteModelInputs | Returns the code to write to root inputs (that is, all model inport blocks). (valid for ERT only) |
LibWriteModelOutputs | Returns the code to write to root outputs (that is, all model outport blocks). (valid for ERT only). |
LibNumDiscreteSampleTimes | Returns the number of discrete sample times in the model. |
LibSetSourceFileCodeTemplate | Set the code template to be used for generating a specified source file. |
LibSetSourceFileOutputDirectory | Set the directory into which a specified source file is to be generated. |
LibAddSourceFileCustomSection | Add a custom section to a source file. The custom section must be associated with one of the built-in (required) sections: Includes, Defines, Types, Enums, Definitions, Declarations, or Functions. |
LibSetSourceFileCustomSection | Adds to the contents of a specified custom section within a specified file. The custom section must have been previously created with LibAddSourceFileCustomSection. |
LibGetSourceFileCustomSection | Returns the contents of a specified custom section within a specified file. |
LibSetCodeTemplateComplianceLevel | This function must be called from your CFP template before any other code template API functions are called. Pass in 2 as the level argument. |
Using code generation template (CGT) files, you can specify custom file banners to be inserted into generated code files. File banners are comment sections in the header and trailer portions of a generated file. You can use these banners to add a company copyright statement, specify a special version symbol for your configuration management system, remove time stamps, and for many other purposes. These banners can contain non US-ASCII characters, which are propagated to the generated code.
The recommended technique for specifying file banners is to create a custom CGT file with a customized banner section. During the build process, an executable TLC file is created from the CGT file. This TLC file is then invoked during the code generation process.
You do not need to be familiar with TLC programming to generate custom banners. Generally, you simply need to modify example files supplied with the ERT target.
Note Prior releases supported direct use of customized TLC file as banner templates. These were specified with the Source file (.c) banner template and Header file (.h) banner template options of the ERT target. Direct use of a TLC file for this purpose is still supported for backward compatibility, but you should now use CGT files for this purpose instead. |
File banner generation is supported by the options in the Code templates section of the Templates pane of the Real-Time Workshop properties of a configuration set (shown in ERT Templates Options).
ERT Templates Options

The options related to file banner generation are
Source file (.c) template: CGT file to use when generating source (.c or .cpp) files. This file must be located on the MATLAB path.
Header file (.h) template: CGT file to use when generating header (.h) files. This file must be located on the MATLAB path. This can be the same template specified in the Source file (.c) template field, in which case identical banners are generated in source and header files.
By default, the template for both source and header files is matlabroot/toolbox/rtw/targets/ecoder/ert_code_template.cgt.
Each of these fields has associated Browse and Edit buttons. Browse lets you navigate to and select an existing CGT file for use as a template. Edit opens the specified file into the MATLAB editor, where you can customize it.
The recommended procedure for customizing a CGT for custom file banner generation is to make a local copy of the default code template and edit it, as follows:
Open the Real-Time Workshop properties view of the active configuration set.
Click on the Templates tab (see ERT Templates Options).
By default, the code template specified in the Source file (.c) template and Header file (.h) template fields is matlabroot/toolbox/rtw/targets/ecoder/ert_code_template.cgt.
If you want to use a different template as your starting point, use the Browse button to locate and select a CGT file.
Click the Edit button to open the CGT file into the MATLAB editor.
Save a local copy of the CGT file. Store the copy in a directory that is outside of the MATLAB directory structure, but on the MATLAB path. If necessary, add the directory to the MATLAB path.
If you intend to use the CGT file in conjunction with a custom target, it is good practice to locate the CGT file in a folder under your target's root directory.
It is also good practice to rename your local copy of the CGT file. When you rename the CGT file, make sure to update the associated Source file (.c) template or Header file (.h) template field to match the new filename.
Edit and customize the local copy of the CGT file for file banner generation, using the information provided in Customizing a Code Generation Template (CGT) File for Custom Banner Generation.
Save your changes to the CGT file.
Generate code. Examine the generated source and/or header files to confirm that they contain the banners specified by the template(s).
This section explains how to edit a CGT file for custom file banner generation. For a general description of CGT files, see Code Generation Template (CGT) Files.
To generate custom file banners, you modify a header section, a trailer section, or both, in a CGT file:
Header section: This section contains comments and tokens for use in generating a custom header banner. The header banner precedes any C or C++ code generated by the model. If the header section is omitted, no header banner is generated. The following is the default header section provided with the default CGT file, matlabroot/toolbox/rtw/targets/ecoder/ert_code_template.cgt.
%% Custom file banner (optional) %% /* * File: %<FileName> * * Real-Time Workshop code generated for Simulink model %<ModelName>. * * Model version : %<ModelVersion> * Real-Time Workshop file version : %<RTWFileVersion> * Real-Time Workshop file generated on : %<RTWFileGeneratedOn> * TLC version : %<TLCVersion> * C source code generated on : %<SourceGeneratedOn> */
Trailer section: This section contains comments and tokens for use in generating a custom trailer banner. The trailer banner follows any C or C++ code generated by the model. If the trailer section is omitted, no trailer banner is generated. The following is the default trailer section provided in the default CGT file.
%% Custom file trailer (optional) %% /* File trailer for Real-Time Workshop generated code. * * [EOF] */
The header and trailer sections typically use TLC variables (such as %<ModelVersion>) as tokens. During code generation, tokens are replaced with values in the generated code. See Summary of Tokens for File Banner Generation for a list of available tokens.
The following code excerpt shows a modified banner section based on the default CGT. This template inserts a copyright notice into the banner.
%% Custom file banner (optional) %% /* * File: %<FileName> * --------------------------------------------------- * Copyright 2008 ABC Corporation, Inc. * --------------------------------------------------- * Real-Time Workshop code generated for Simulink model %<ModelName>. * * Model version : %<ModelVersion> * Real-Time Workshop file version : %<RTWFileVersion> * Real-Time Workshop file generated on : %<RTWFileGeneratedOn> * TLC version : %<TLCVersion> * C source code generated on : %<SourceGeneratedOn> * */
The following code excerpt shows a file banner generated from the rtwdemo_udt model using the above template.
/* * File: rtwdemo_udt.c * --------------------------------------------------- * Copyright 2008 ABC Corporation, Inc. * --------------------------------------------------- * Real-Time Workshop code generated for Simulink model rtwdemo_udt. * * Model version : 1.116 * Real-Time Workshop file version : 7.1 (R2008a) 25-Dec-2007 * Real-Time Workshop file generated on : Thu Jan 17 17:14:16 2008 * TLC version : 7.1 (Jan 9 2008) * C source code generated on : Thu Jan 17 17:14:16 2008 * */
Summary of Tokens for File Banner Generation
FileName | Name of the generated file (for example, "rtwdemo_udt.c"). |
FileType | Either "source" or "header". Designates whether generated file is a .c or .cpp file or an .h file. |
FileTag | Given filenames file.c or .cpp and file.h, the file tags are "file_c" and "file_h", respectively. |
ModelName | Name of generating model. |
ModelVersion | Version number of model. |
RTWFileVersion | Version number of model.rtw file. |
RTWFileGeneratedOn | Timestamp of model.rtw file. |
TLCVersion | Version of Target Language Compiler. |
SourceGeneratedOn | Timestamp of generated file. |
![]() | Generating Efficient Code with Optimized ERT Targets | Optimizing Your Model with Configuration Wizard Blocks and Scripts | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |