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:
Open a Simulink model.
openExample('ex_param_array');
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
orSimulink.Parameter
, which are defined in the packageSimulink
.myParam = Simulink.Parameter(15.23);
On the Simulink® toolstrip, on the Simulation tab, in the Prepare section, select Property Inspector. Click the target Gain block. In the Property Inspector pane, set the Gain 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
ExportedGlobal
.myParam.StorageClass = 'ExportedGlobal';
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';
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. */
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:
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 classGlobal (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.Write a MATLAB or TLC function that generates the comment text.
For an example MATLAB® function, open the
CustomCodeComments
model and navigate to the Custom comments function configuration parameter:openExample(‘CustomCodeComments’)
The function must accept three input arguments that correspond to
objectName
,modelName
, andrequest
. If you write a TLC file, you can use the library functionLibGetSLDataObjectInfo
to get the property values of the data object.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 */
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.