If you have a Simulink^{®} Coder™ license, you can include MATLAB^{®} source code as comments in the code generated for a MATLAB Function block. Including this information in the generated code enables you to:
Correlate the generated code with your source code.
Understand how the generated code implements your algorithm.
Evaluate the quality of the generated code.
When you select this option, the generated code includes:
The source code as a comment immediately after the traceability tag. When you enable traceability and generate code for ERT targets (requires an Embedded Coder^{®} license), the traceability tags are hyperlinks to the source code. For more information on traceability for the MATLAB Function block, see Use Traceability in MATLAB Function Blocks.
For examples and information on the location of the comments in the generated code, see Location of Comments in Generated Code.
The function help text in the function body in the generated code. The function help text is the first comment after the MATLAB function signature. It provides information about the capabilities of the function and how to use it.
Note: With an Embedded Coder license, you can also include the function help text in the function banner of the generated code. For more information, see Including MATLAB Function Help Text in the Function Banner. |
To include MATLAB source code as comments in the code generated for a MATLAB Function block:
In the model, select Simulation > Model Configuration Parameters.
In the Code Generation > Comments pane, select MATLAB source code as comments and click Apply.
The automatically generated comments containing the source code appear after the traceability tag in the generated code as follows.
The comment containing the source code precedes the generated code that implements the source code statement. This comment appears after any comments that you add that precede the generated code. The comments are separated from the generated code because the statements are assigned to function outputs.
MATLAB Code
function [x y] = straightline(r,theta) %#codegen % Convert polar to Cartesian x = r * cos(theta); y = r * sin(theta);
Commented C Code
/* MATLAB Function 'straightline': '<S1>:1' */ /* Convert polar to Cartesian */ /* '<S1>:1:4' x = r * cos(theta); */ /* '<S1>:1:5' y = r * sin(theta); */ straightline0_Y.x = straightline0_U.r * cos(straightline0_U.theta); /* Outport: '<Root>/y' incorporates: * Inport: '<Root>/r' * Inport: '<Root>/theta' * MATLAB Function Block: '<Root>/straightline' */ straightline0_Y.y = straightline0_U.r * sin(straightline0_U.theta);
The comment for the if
statement immediately
precedes the code that implements the statement. This comment appears
after any comments that you add that precede the generated code.
The comments for the elseif
and else
clauses
appear immediately after the code that implements the clause, and
before the code generated for statements in the clause.
MATLAB Code
function y = ifstmt(u,v) %#codegen if u > v y = v + 10; elseif u == v y = u * 2; else y = v - 10; end
Commented C Code
/* MATLAB Function 'MLFcn': '<S1>:1' */ /* '<S1>:1:3' if u > v */ if (MLFcn_U.u > MLFcn_U.v) { /* Outport: '<Root>/y' */ /* '<S1>:1:4' y = v + 10; */ MLFcn_Y.y = MLFcn_U.v + 10.0; } else if (MLFcn_U.u == MLFcn_U.v) { /* Outport: '<Root>/y' */ /* '<S1>:1:5' elseif u == v */ /* '<S1>:1:6' y = u * 2; */ MLFcn_Y.y = MLFcn_U.u * 2.0; } else { /* Outport: '<Root>/y' */ /* '<S1>:1:7' else */ /* '<S1>:1:8' y = v - 10; */ MLFcn_Y.y = MLFcn_U.v - 10.0;
The comment for the for
statement header
immediately precedes the generated code that implements the header.
This comment appears after any comments that you add that precede
the generated code.
MATLAB Code
function y = forstmt(u) %#codegen y = 0; for i=1:u y = y + 1; end
Commented C Code
/* MATLAB Function 'MLFcn': '<S1>:1' */ /* '<S1>:1:3' y = 0; */ rtb_y = 0.0; /* '<S1>:1:5' for i=1:u */ for (i = 1.0; i <= MLFcn_U.u; i++) { /* '<S1>:1:6' y = y + 1; */ rtb_y++;
The comment for the while
statement header
immediately precedes the generated code that implements the statement
header. This comment appears after any comments that you add that
precede the generated code.
The comment for the switch
statement header
immediately precedes the generated code that implements the statement
header. This comment appears after any comments that you add that
precede the generated code. The comments for the case
and otherwise
clauses
appear immediately after the generated code that implements the clause,
and before the code generated for statements in the clause.
You can include the function help text in the function banner of the code generated for a MATLAB Function block. The function help text is the first comment after the MATLAB function signature. It provides information about the capabilities of the function and how to use it.
In the model, select Simulation > Model Configuration Parameters.
In the Code Generation > Comments pane, select MATLAB function help text and click Apply.
Note: If the function is inlined, the function help text is also inlined. Therefore, the help text for inlined functions appears in the function body in the generated code even when this option is selected. |
The MATLAB Function block has the following limitations for including MATLAB source code as comments.
You cannot include MATLAB source code as comments for:
MathWorks^{®} toolbox functions
P-code
Simulation targets
Stateflow^{®} Truth Table blocks
The appearance or location of comments can vary depending on the following conditions:
Comments might still appear in the generated code even if the implementation code is eliminated, for example, due to constant folding.
Comments might be eliminated from the generated code if a complete function or code block is eliminated.
For certain optimizations, the comments might be separated from the generated code.
The generated code always includes legally required comments from the MATLAB source code, even if you do not choose to include source code comments in the generated code.