Main Content

If you have Simulink^{®}
Coder™ , 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 the **MATLAB source code as comments** parameter, 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 Embedded Coder

^{®}), the traceability tags are hyperlinks to the source code. For more information, 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 Embedded Coder, you can also include the function help text in the generated code function banner. For more information, see Include MATLAB User Comments in Generated Code.

To include MATLAB source code as comments in the code generated for a MATLAB Function block:

On the

**C Code**tab, click**Configuration Parameters**.In the

**Code Generation > Comments**pane, select**MATLAB source code as comments**and click**Apply**.

The comments containing the source code appear after the traceability tag in the generated code as follows.

/* '<S2>:1:18' for y = 1 : 2 : (HEIGHT-4) */

`'<S2>:1:18'`

. Selecting the
`for y = 1 : 2 : (HEIGHT-4)`

comment.The comment containing the source code precedes the generated code that implements the source code statement. This comment appears after comments that you add, which 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 comments that you
add, which 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, which implements the header. This comment appears
after comments that you add, which 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, which implements the statement header. This comment
appears after comments that you add, which 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 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.

MATLAB user comments include the function help text and other comments. 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. You can include the MATLAB user comments in the code generated for a MATLAB Function block.

On the

**C Code**tab, click**Configuration Parameters**.In the

**Code Generation > Comments**pane, select MATLAB user comments and click**Apply**.

For including MATLAB source code as comments, the MATLAB Function block has these limitations :

You cannot include MATLAB source code as comments for:

MathWorks

^{®}toolbox functionsP-code

Simulation targets

Stateflow

^{®}Truth Table blocks

The appearance or location of comments can vary depending on these 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 includes legally required comments from the MATLAB source code, even if you do not choose to include source code comments in the generated code.