Main Content

Generate Custom File and Function Banners for C/C++ Code

When you generate C and C++ code from MATLAB® code, you can use a code generation template (CGT) file to specify custom:

  • File banners

  • Function Banners

  • File trailers

  • Comments before code sections

This example shows how you can create your own CGT file and customize it to generate your own file and function banners.

  1. Create a local copy of the default CGT file for MATLAB Coder™ and rename it. The default CGT file is matlabcoder_default_template.cgt in the matlabroot/toolbox/coder/matlabcoder/templates/ folder.

  2. Store the copy in a folder that is outside of the MATLAB folder structure, but on the MATLAB path. If necessary, add the folder to the MATLAB path. If you intend to use the CGT file with a custom target, locate the CGT file in a folder under your target root folder. If the file is not on the MATLAB path, specify a full path to the file when adding the file to your configuration.

  3. View the default template and generated output. For example, here is the default File Banner section:

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %% 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>
    When you generate code using this default, the file banner looks similar to this file banner:
    /*
     * File: coderand.c
     *
     * MATLAB Coder version            : 2.7
     * C/C++ source code generated on  : 06-Apr-2014 14:34:15
     */
    

  4. Edit your local copy of the CGT file. You can change the default values and add your own custom tokens. For example, here is the File Banner section with the style changed to box and a custom token myCustomToken:

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %% 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="box">
    File: %<FileName>
    
    My custom token                : %<myCustomToken>
    
    MATLAB Coder version           : %<MATLABCoderVersion>
    C/C++ source code generated on : %<SourceGeneratedOn>
    </FileBanner>

    For more information, see Code Generation Template Files for MATLAB Code.

  5. Create a configuration object for generation of a C static library for an embedded target.

    % Create configuration object for an embedded target
    cfgObj = coder.config('lib','ecoder',true); 
    

  6. Create a MATLABCodeTemplate object from your CGT file and add it to the configuration object.

    % Specify the custom CGT file
    CGTFile = 'myCGTFile.cgt';
    % Use custom template
    cfgObj.CodeTemplate = coder.MATLABCodeTemplate(CGTFile); 
    
  7. Assign values for custom tokens that you added to the template. For example, assign the value 'myValue' to the myCustomToken token that you added in a previous step.

    cfgObj.CodeTemplate.setTokenValue('myCustomToken','myValue');

  8. Make sure that generation of comments is enabled. Otherwise, the code generator does not produce file or function banners.

    cfgObj.GenerateComments=true;

  9. Generate code using the configuration object that you created.

    codegen -config cfgObj coderand

  10. View the changes to the generated file banner. For example, here is the file banner for coderand.c using the customized CGT file:

    /******************************************************************************/
    /* File: coderand.c                                                           */
    /*                                                                            */
    /* My custom token                : myValue                                   */
    /*                                                                            */
    /* MATLAB Coder version           : 2.7                                       */
    /* C/C++ source code generated on : 06-Apr-2014 14:42:55                      */
    /******************************************************************************/
    

Changes to a CGT file do not affect the generated code unless you create a MATLABCodeTemplate object from the modified CGT file, and then add it to the configuration object. If you modify the CGT File, myCGTFile.cgt, used in the previous example, you must repeat these steps:

  1. Create a MATLABCodeTemplate object from myCGTFile.cgt and add it to the configuration object.

    CGTFile = 'myCGTFile.cgt';
    cfgObj.CodeTemplate = coder.MATLABCodeTemplate(CGTFile); 
    
  2. Assign the value 'myValue' to the myCustomToken token.

    cfgObj.CodeTemplate.setTokenValue('myCustomToken','myValue');

  3. Generate code.

    codegen -config cfgObj coderand

See Also

Related Topics