Tracing Generated C/C++ Code to MATLAB Source Code - MATLAB & Simulink (original) (raw)

Tracing the generated C/C++ code to the original MATLAB® source code helps you to:

You can trace by using one of these methods:

Generate Traceability Tags

To produce traceability tags in the generated code, enable generation of MATLAB source code as comments.

Format of Traceability Tags

In the generated code, traceability tags appear immediately before the MATLAB source code in the comment. The format of the tag is:
<filename>:<line number>.

For example, this comment indicates that the code x = r * cos(theta); appears at line 4 in the source file straightline.m.

/* 'straightline:4' x = r * cos(theta); */

Location of Comments in Generated Code

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

Straight-Line Source Code

In straight-line source code without if, while,for or switch statements, the comment containing the source code precedes the generated code that implements the source code statement. This comment appears after user comments that precede the generated code.

For example, in the following code, the user comment, /* Convert polar to Cartesian */, appears before the generated comment containing the first line of source code, together with its traceability tag,
/* 'straightline:4' x = r * cos(theta); */.

MATLAB Code

function [x, y] = straightline(r,theta) %#codegen % Convert polar to Cartesian x = r * cos(theta); y = r * sin(theta);

Commented C Code

void straightline(double r, double theta, double *x, double y) { / Convert polar to Cartesian / / 'straightline:4' x = r * cos(theta); */ *x = r * cos(theta);

/* 'straightline:5' y = r * sin(theta); */ *y = r * sin(theta); }

If Statements

The comment for the if statement immediately precedes the code that implements the statement. This comment appears after user comments 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

double ifstmt(double u, double v) { double y;

/* 'ifstmt:3' if u > v / if (u > v) { / 'ifstmt:4' y = v + 10; / y = v + 10.0; } else if (u == v) { / 'ifstmt:5' elseif u == v / / 'ifstmt:6' y = u * 2; / y = u * 2.0; } else { / 'ifstmt:7' else / / 'ifstmt:8' y = v - 10; */ y = v - 10.0; }

return y; }

For Statements

The comment for the for statement header immediately precedes the generated code that implements the header. This comment appears after user comments 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

double forstmt(double u) { double y; int i;

/* 'forstmt:3' y = 0; */ y = 0.0;

/* 'forstmt:4' for i = 1:u / for (i = 0; i < (int)u; i++) { / 'forstmt:5' y = y + 1; */ y++; }

return y; }

While Statements

The comment for the while statement header immediately precedes the generated code that implements the statement header. This comment appears after user comments that precede the generated code.

MATLAB Code

function y = subfcn(y) coder.inline('never'); while y < 100 y = y + 1; end

Commented C Code

void subfcn(double y) { / 'subfcn:2' coder.inline('never'); / / 'subfcn:3' while y < 100 */ while (y < 100.0) { / 'subfcn:4' y = y + 1; */ (*y)++; } }

Switch Statements

The comment for the switch statement header immediately precedes the generated code that implements the statement header. This comment appears after user comments that precede the generated code. The comments for the case andotherwise clauses appear immediately after the generated code that implements the clause, and before the code generated for statements in the clause.

MATLAB Code

function y = switchstmt(u) %#codegen y = 0; switch u case 1 y = y + 1; case 3 y = y + 2; otherwise y = y - 1; end

Commented C Code

double switchstmt(double u) { double y;

/* 'switchstmt:3' y = 0; / / 'switchstmt:4' switch u / switch ((int)u) { case 1: / 'switchstmt:5' case 1 / / 'switchstmt:6' y = y + 1; */ y = 1.0; break;

case 3: /* 'switchstmt:7' case 3 / / 'switchstmt:8' y = y + 2; */ y = 2.0; break;

default: /* 'switchstmt:9' otherwise / / 'switchstmt:10' y = y - 1; */ y = -1.0; break; }

return y; }

Traceability Tag Limitations