Skip to Main Content Skip to Search
Product Documentation

Including MATLAB Source Code as Comments in the Generated Code

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:

When you select this option, the generated code includes:

How to 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. In the model, select Simulation > Configuration Parameters.

  2. In the Code Generation > Comments pane, select MATLAB code as comments and click Apply.

Location of Comments in Generated Code

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

Straight-Line Source Code

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);

If Statements

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;

For Statements

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++;

While Statements

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.

Switch Statements

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.

Including MATLAB Function Help Text in the Function Banner

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.

  1. In the model, select Simulation > Configuration Parameters.

  2. In the Code Generation > Comments pane, select MATLAB function help text and click Apply.

Limitations of MATLAB Source Code as Comments

The MATLAB Function block has the following limitations for including MATLAB source code as comments.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS