Main Content

Code Generation Template Files for MATLAB Code

A code generation template (CGT) file defines the sections in generated code that you can customize using comments and tokens. Using a code generation template (CGT) file for the generation of C and C++ code from MATLAB® code, you can specify custom file banners and function banners for generated code. File banners are comment sections in the header and trailer sections of a generated file. Function banners are comment sections for each function in the generated code. You can also customize comments before code sections. Use these banners to:

  • Add a company copyright statement.

  • Specify a special version symbol for your configuration management system.

  • Remove timestamps.

  • Add other custom information to your generated files.

For information on creating, customizing, and using a CGT file, see Generate Custom File and Function Banners for C/C++ Code.

Default CGT File

You can base your custom template on the default CGT file, matlabcoder_default_template.cgt, in the matlabroot/toolbox/coder/matlabcoder/templates/ folder.

Note

If you choose not to customize banners for your generated code, the default template is used for code generation.

CGT File Structure

A CGT file consists of 13 optional sections.

File Banner Section

Contains comments and tokens for use in generating a custom file banner.

Function Banner Section

Contains comments and tokens for use in generating a custom function banner.

Shared Utility Function Banner

Contains comments and tokens for use in generating custom banners for shared utility functions.

File Trailer Section

Contains comments for use in generating a custom trailer banner.

Include Files Banner

Contains comments for use in generating a custom banner for the include files section.

Type Definitions

Contains comments for use in generating a custom banner for the type definitions section.

Named Constants

Contains comments for use in generating a custom banner for the named constants section.

Variable Declarations

Contains comments for use in generating a custom banner for the variable declarations section.

Variable Definitions

Contains comments for use in generating a custom banner for the variable definitions section.

Function Declarations

Contains comments for use in generating a custom banner for the function declarations section.

Function Definitions

Contains comments for use in generating a custom banner for the function definitions section.

Custom Source Code

Contains comments for use in generating a custom banner for the custom source code section.

Custom Header Code

Contains comments for use in generating a custom banner for the custom header code section.

Components of the CGT File Sections

Each CGT file section is defined by open and close tags.

CGT File SectionOpen TagClose Tag
File Banner<FileBanner></FileBanner>
Function Banner Section<FunctionBanner></FunctionBanner>
Shared Utility Function Banner<SharedUtilityBanner></SharedUtilityBanner>
File Trailer Section<FileTrailer></FileTrailer>
Include Files Banner<IncludeFilesBanner></IncludeFilesBanner>
Type Definitions<TypeDefinitionsBanner></TypeDefinitionsBanner>
Named Constants<NamedConstantsBanner></NamedConstantsBanner>
Variable Declarations<VariableDeclarationsBanner></VariableDeclarationsBanner>
Variable Definitions<VariableDefinitionsBanner></VariableDefinitionsBanner>
Function Declarations<FunctionDeclarationsBanner></FunctionDeclarationsBanner>
Function Definitions<FunctionDefinitionsBanner></FunctionDefinitionsBanner>
Custom Source Code<CustomSourceCodeBanner></CustomSourceCodeBanner>
Custom Header Code<CustomHeaderCodeBanner></CustomHeaderCodeBanner>

You can customize your banners by including tokens and comments between the open and close tags for each section. Tokens are replaced with values in the generated code. The following rules apply to tokens in your CGT file:

  • You can have only one token per line.

  • Token values must not contain a ‘\t’ for formatting.

Note

In the contents of your banner, C comment indicators, '/*' or '*/', can introduce an error in the generated code.

An open tag includes tag attributes. Enclose the value of the attribute in double quotes. The attributes available for an open tag are:

  • width: specifies the width of the file or function banner comments in the generated code. The default value is 80.

  • style: specifies the boundary for the file or function banner comments in the generated code.

The open tag syntax is:

<OpenTag style = “style_value” width = “num_width”>

There are five options for the banner style. The CommentStyle and TargetLang configuration object properties determine the use of C or C++ comment style. The built-in style options for the style attribute are:

  • classic

    Using C style comments

    /* single line comments */
    
    /*
     * multiple line comments
     * second line
     */

    Using C++ style comments

    // single line comments
    
    //
    // multiple line comments
    // second line
    //

  • box

    Using C style comments

    /**********************************************************/
    /* banner contents                                        */
    /**********************************************************/

    Using C++ style comments

    ////////////////////////////////////////////////////////////
    // banner contents                                        //
    ////////////////////////////////////////////////////////////

  • open_box

    Using C style comments

    /**********************************************************
     * banner contents
     **********************************************************/

    Using C++ style comments

    ////////////////////////////////////////////////////////////
    // banner contents
    ////////////////////////////////////////////////////////////

  • doxygen

    Using C style comments

    /** single line comments */
    
    /**
     * multiple line comments
     * second line
     */

    Using C++ style comments

    ///single line comments
    
    ///
    /// multiple line comments
    ///second line
    ///

  • doxygen_qt

    Using C style comments

    /*! single line comments */
    
    /*!
     * multiple line comments
     * second line
     */

    Using C++ style comments

    //!single line comments
    
    //!
    //! multiple line comments
    //!second line
    //!

File Banner

This section contains comments and tokens for use in generating a custom file banner that precedes the generated C and C++ code. If you omit the file banner section from the CGT file, the code generator does not generate a file banner in the generated code. The file banner section provided in the default CGT file is:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Custom File Banner section (optional)
%% Customize File banners by using either custom tokens or the following
%% predefined tokens: 
%% %<FileName>, %<MATLABCoderVersion>, %<EmbeddedCoderVersion>
%% %<SourceGeneratedOn>, %<HardwareSelection>, %<OutputType>
%%
%% You can also use "custom tokens" in all of the sections below. See the 
%% documentation center for more details. 
%%
<FileBanner style="classic">
File: %<FileName>

MATLAB Coder version           : %<MATLABCoderVersion>
C/C++ source code generated on : %<SourceGeneratedOn>
</FileBanner>

Summary of Tokens for File Banner Generation

FileName

Name of the generated file (for example, "kalman.c")

SourceGeneratedOn

Timestamp of generated file

MATLABCoderVersion

Version of MATLAB Coder™

EmbeddedCoderVersion

Version of Embedded Coder®

HardwareSelection

Selected target

OutputType

Type of output (for example, lib, exe, or dll)

Function Banner

This section contains comments and tokens for use in generating a custom function banner that precedes a generated C or C++ function. If you omit the function banner section from the CGT file, the code generator does not generate function banners. The function banner section provided in the default CGT file is:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Custom Function Banner section (optional)
%% Customize function banners by using the following predefined tokens: 
%% %<FunctionName>, %<FunctionDescription>, %<FunctionPrototype>
%% %<Arguments>, %<ReturnType> 
%%
<FunctionBanner style="classic">
%<FunctionPrototype>
%<FunctionDescription>
Arguments    : %<Arguments>
Return Type  : %<ReturnType>
</FunctionBanner>

Summary of Tokens for Function Banner Generation

FunctionName

Name of function

FunctionDescription

Short abstract about the function

If the configuration parameter MATLABFcnDesc is true, the function description appears in the function banner. Otherwise, the code generator treats the function description as user comments.

FunctionPrototype

MATLAB function prototype

The function prototype appears in the function banner only if the MATLABSourceComments configuration parameter is true.

Arguments

List of function arguments

ReturnType

Return type of function

Shared Utility Banner

This section contains comments and tokens for use in generating a custom shared utility function banner that precedes a generated C or C++ shared utility function. If you omit the shared utility function banner section from the CGT file, the code generator does not generate shared utility function banners. The shared utility function banner section provided in the default CGT file is:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Custom Shared Utility Function Banner section (optional)
%% Customize shared utility function banners by using the following 
%% predefined tokens: 
%% %<FunctionName>, %<FunctionDescription>
%% %<Arguments>, %<ReturnType>
%%
<SharedUtilityBanner style="classic">
Arguments    : %<Arguments>
Return Type  : %<ReturnType> 
</SharedUtilityBanner>

Summary of Tokens for Shared Utility Function Banner Generation

FunctionName

Name of function

FunctionDescription

Short abstract about the function

Arguments

List of function arguments

ReturnType

Return type of function

File Trailer

The file trailer section contains comments for generating a custom file trailer that follows the generated C or C++ code. If you omit the file trailer section from the CGT file, the code generator does not generate a file trailer. The file trailer section provided in the default CGT file is:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Custom file trailer section (optional)
%% You can use any of the predefined tokens used for File Banner
%%
<FileTrailer style="classic">
File trailer for %<FileName>

[EOF]
</FileTrailer>

Tokens for the file banner are available for the file trailer. See Summary of Tokens for File Banner Generation.

Include Files Banner

The include files banner section contains comments for generating a custom banner that precedes the include files section in the generated code. If you omit the include files banner section from the CGT file, the code generator does not generate a banner for this section. The include files banner section provided in the default CGT file is:

<IncludeFilesBanner  style="classic">
Include Files
</IncludeFilesBanner>

Type Definitions Banner

The type definitions banner section contains comments for generating a custom banner that precedes the type definitions section in the generated code. If you omit the type definitions banner section from the CGT file, the code generator does not generate a banner for this section. The type definitions banner section provided in the default CGT file is:

<TypeDefinitionsBanner  style="classic">
Type Definitions
</TypeDefinitionsBanner>

Named Constants Banner

The named constants banner section contains comments for generating a custom banner that precedes the named constants section in the generated code. If you omit the named constants banner section from the CGT file, the code generator does not generate a banner for this section. The named constants banner section provided in the default CGT file is:

<NamedConstantsBanner  style="classic">
Named Constants
</NamedConstantsBanner>

Variable Declarations

The variable declarations banner section contains comments for generating a custom banner that precedes the variable declarations section in the generated code. If you omit the variable declarations banner section from the CGT file, the code generator does not generate a banner for this section. The variable declarations banner section provided in the default CGT file is:

<VariableDeclarationsBanner  style="classic">
Variable Declarations
</VariableDeclarationsBanner>

Variable Definitions

The variable definitions banner section contains comments for generating a custom banner that precedes the variable definitions section in the generated code. If you omit the variable definitions banner section from the CGT file, the code generator does not generate a banner for this section. The variable definitions banner section provided in the default CGT file is:

<VariableDefinitionsBanner  style="classic">
Variable Definitions
</VariableDefinitionsBanner>

Function Declarations

The function declarations banner section contains comments for generating a custom banner that precedes the function declarations section in the generated code. If you omit the function declarations banner section from the CGT file, the code generator does not generate a banner for this section. The function declarations banner section provided in the default CGT file is:

<functionDeclarationsBanner  style="classic">
Function Declarations
</FunctionDeclarationsBanner>

Function Definitions

The function definitions banner section contains comments for generating a custom banner that precedes the function definitions section in the generated code. If you omit the function definitions banner section from the CGT file, the code generator does not generate a banner for this section. The function definitions banner section provided in the default CGT file is:

<FunctionDefinitionsBanner  style="classic">
Function Definitions
</FunctionDefinitionsBanner>

Custom Source Code

The custom source code banner section contains comments for generating a custom banner that precedes the custom source code section in the generated code. If you omit the custom source code banner section from the CGT file, the code generator does not generate a banner for this section. The custom source code banner section provided in the default CGT file is:

<CustomSourceCodeBanner  style="classic">
Custom Source Code
</CustomSourceCodeBanner>

Custom Header Code

The custom header code banner section contains comments for generating a custom banner that precedes the custom header code section in the generated code. If you omit the custom header code banner section from the CGT file, the code generator does not generate a banner for this section. The custom header code banner section provided in the default CGT file is:

<CustomHeaderCodeBanner  style="classic">
Custom Header Code
</CustomHeaderCodeBanner>

See Also

Related Topics