Include MATLAB Code as Comments in Generated Code - MATLAB & Simulink (original) (raw)

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:

When you select the MATLAB source code as comments parameter, the generated code includes:

Include MATLAB Code as Comments in the Generated Code

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

  1. On the C Code tab, click Configuration Parameters.
  2. In the Code Generation > Comments pane, selectMATLAB source code as comments and clickApply.

Location of Comments in Generated Code

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

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

Selecting the Stateflow object comments parameter generates the traceability comment '<S2>:1:18'. Selecting theMATLAB source code as comments parameter generates thefor y = 1 : 2 : (HEIGHT-4) comment.

Straight-Line Source Code

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': ':1' / / Convert polar to Cartesian / / ':1:4' x = r * cos(theta); / / ':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);

If Statements

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 theelseif 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': ':1' / / ':1:3' if u > v / if (MLFcn_U.u > MLFcn_U.v) { / Outport: '/y' / / ':1:4' y = v + 10; / MLFcn_Y.y = MLFcn_U.v + 10.0; } else if (MLFcn_U.u == MLFcn_U.v) { / Outport: '/y' / / ':1:5' elseif u == v / / ':1:6' y = u * 2; / MLFcn_Y.y = MLFcn_U.u * 2.0; } else { / Outport: '/y' / / ':1:7' else / / ':1:8' y = v - 10; */ MLFcn_Y.y = MLFcn_U.v - 10.0;

For Statements

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': ':1' / / ':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++;

While Statements

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.

Switch Statements

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.

Include MATLAB User Comments in Generated Code

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.

  1. On the C Code tab, click Configuration Parameters.
  2. In the Code Generation > Comments pane, selectMATLAB user comments (Embedded Coder) and click Apply.

Limitations of MATLAB Source Code as Comments

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

See Also

Topics