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.
To embed handwritten comments in the generated code near the definition of a signal, state, or parameter:
Open a Simulink model.
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
which are defined in the package
myParam = Simulink.Parameter(15.23);
In the model, under the PREPARE section select Property Inspector. Click the target Gain block. Set its parameter value to myParam.
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
myParam.StorageClass = 'ExportedGlobal';
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';
In the Model Configuration Parameters select Simulink data object descriptions.
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. */
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
for the Simulink model in the previous example:
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
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
Write a MATLAB or TLC function that generates the comment text. For an example
MATLAB® function, see the function
The function must accept three input arguments that correspond to
request. If you write a TLC file, you can use the library
LibGetSLDataObjectInfo to get the property values of the
Save the function as a MATLAB file or a TLC file. Place the file in a folder that is on your MATLAB path.
In the Model Configuration Parameters select Custom comments (MPT objects only).
Set Custom comments function to the name of the MATLAB file or TLC file that you created.
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 */
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.