Main Content

Add Custom Comments for Variables in the Generated Code

To control code generation options for signals, states, and parameters in a model, you can create data objects in a workspace or data dictionary. You can generate comments in the code that help you to document the purpose and properties of the data in each object. Associate handwritten comments with each object, or write a function that generates comments based on the properties of the object.

For more information about data objects, see Data Objects.

Embed Handwritten Comments for Signals or Parameters

To embed handwritten comments in the generated code near the definition of a signal, state, or parameter:

  1. Open a Simulink model.

    model='ex_param_array';
    open_system(model)

  2. Create a data object to represent a signal, state, or parameter. You can use a data object from any package such as Simulink. For example, use a data object of the classes Simulink.Signal or Simulink.Parameter, which are defined in the package Simulink.

    myParam = Simulink.Parameter(15.23);

  3. In the model, under the PREPARE section select Property Inspector. Click the target Gain block. Set its parameter value to myParam.

  4. Set the storage class of the data object so that optimizations do not eliminate the signal or parameter from the generated code. For example, use the storage class ExportedGlobal.

    myParam.StorageClass = 'ExportedGlobal';

  5. Set the Description property of the object. The description that you specify appears in the generated code as lines of comments.

    myParam.Description = 'This parameter represents multiplication';

  6. In the Model Configuration Parameters select Simulink data object descriptions.

  7. Generate code from the model. In the code, the data object description appears near the definition of the corresponding variable.

    /* Exported block parameters */
    real_T myParam = 15.23;     /* Variable: myParam
                                 * Referenced by: '<S1>/Gain'
                                 * This parameter represents multiplication.
                                 */

Generate Dynamic Comments Based on Data Properties

You can generate dynamic comments that include the properties of a data object such as data type, units, and dimensions. If you change the properties of the data object in Simulink®, the code generator maintains the accuracy of the comments. For example, this comment displays some of the property values for a data object named MAP for the Simulink model in the previous example:

  1. Create a data object from the package mpt and apply a custom storage class to the object. The default storage class is the custom storage class Global (Custom).

    myParam = mpt.Parameter;

    To generate dynamic comments, you must use a data object from the package mpt and you must apply a custom storage class to the object.

  2. Write a MATLAB or TLC function that generates the comment text. For an example MATLAB® function, see the function matlabroot/toolbox/rtw/rtwdemos/rtwdemo_comments_mptfun.m.

    The function must accept three input arguments that correspond to objectName, modelName, and request. If you write a TLC file, you can use the library function LibGetSLDataObjectInfo to get the property values of the data object.

  3. Save the function as a MATLAB file or a TLC file. Place the file in a folder that is on your MATLAB path.

  4. In the Model Configuration Parameters select Custom comments (MPT objects only) i.

  5. Set Custom comments function to the name of the MATLAB file or TLC file that you created.

  6. Generate code from the model. The comments that your function generates appear near the code that represents each data object.

    /* Object: myParam - user description:
       DataType -- auto
       Units    --
       CSC      -- Global */

Limitations

  • To generate comments by using the Custom comments (MPT objects only) and Custom comments function options, you must create data objects from the package mpt. The data objects must use a custom storage class.

  • Only the custom storage classes from the mpt package that create unstructured variables support the custom comments function.

Related Examples

More About