Main Content

Add Global Comments in the Generated Code

These examples show how to add a global comment to a Simulink® model so that the comment text appears in the generated file or files. Specify a template symbol name with a Simulink DocBlock, a Simulink annotation, or a Stateflow® note. You can also use a sorted-notes capability that works with Simulink annotations or Stateflow notes, but not DocBlocks. For more information about template symbols, see Template Symbols and Rules.

Note

Template symbol names Description and Model Information are fields in the Property Inspector dialog box. If you use one of these symbol names for global comment text and its Property Inspector fields also has text in it, both names appear in the generated files.

Use a Simulink DocBlock to Add a Comment

  1. Open a Simulink model.

    model='rtwdemo_codetemplate';
    open_system(model)

  2. Under Configuration Parameters, set the Code templates to rtwdemocodetemplate.cgt. This code template contains the required customizations to generate global comments.

  3. Right-click the DocBlock and select Mask > Mask Parameters.

  4. In the Code generation template symbol box, type one of the following:

    • Abstract

    • Description

    • History

    • Notes

    Click OK. Template symbol names are case-sensitive.

    If you are using a DocBlock to add comments to your code, set the Document type to Text. If you set Document type to RTF or HTML, your comments do not appear in the code.

  5. In the Block Properties dialog box, on the Block Annotation tab, select %<ECoderFlag> and click OK. The symbol name that you typed in the previous step now appears under the DocBlock in the model.

  6. Save the model. After you generate code, the code generator places the comment in each generated file whose template has the symbol name that you designated. The code generator places the comment in the generated file at the location that corresponds to where the symbol name is located in the template file.

    The relevant fragment from the generated file for this model is:

    	 *
    	 * ABSTRACT:
    	 * This text maps to the code template %<Abstract> symbol.

  7. To add more comments to the generated files, repeat steps 2–6.

Use a Simulink Annotation to Add a Comment

  1. Open the model rtwdemo_codetemplate.

  2. Double-click the unoccupied area on the model where you want to place the comment. See Describe Models Using Notes and Annotations.

  3. Type <S:Symbol_name> followed by the comment. Symbol_name is one of the following:

    • Abstract

    • Description

    • History

    • Notes

    For example, type <S:Description>This is the description I want. Template symbol names are case-sensitive. If you want the code generator to sort multiple comments for the Notes symbol name, replace the next step with Use Sorted Notes to Add Comments.

  4. Click outside the annotation rectangle and save the model. After you generate code, the code generator places the comment in each generated file whose template has the symbol name that you assigned. The code generator places the comment in the generated file at the location that corresponds to where the symbol name is located in the template file. If you want the code generator to sort multiple comments for the Notes symbol name, replace the next step with Use Sorted Notes to Add Comments.

  5. To add one or more other comments to the generated files, repeat steps 2–4.

Use Sorted Notes to Add Comments

The sorted-notes capability enables you to add sorted comments to the generated files. The code generator places these comments in each generated file at the location that corresponds to where the Notes symbol is located in the template file.

The code generator uses this sorting order:

  • Numbers before letters.

  • Among numbers, zero is placed first.

  • Among letters, uppercase letters are placed before lowercase letters.

You can use sorted notes for a Simulink annotation or a Stateflow note, but not with a DocBlock.

  • In the Simulink annotation or the Stateflow note, type <S:NoteY> followed by the first comment. Y is a number or a letter.

  • Repeat this process for as many additional comments you want. Replace Y with a subsequent number or letter.

This figure illustrates sorted notes on a model. It shows where the code generator places each note in a generated file.

The relevant fragment from the generated file for this model is:

	 *
	 * NOTES:
	 * Note: This Simulink annotation maps to the code template %<Note> symbol.
	 * Note1: Another Simulink annotation .
	 *

Limitations

You cannot generate global comments using template symbol names inside a Simulink DocBlock, when the Language for code generation is C++.

Related Topics