Requirements Traceability for Code Generated from MATLAB Code
When you generate C/C++ code from MATLAB® code that has links to requirements, you can include comments in the generated code that contain information about the requirements and the linked MATLAB code ranges. When you view the generated code from a code generation report, the comments are hyperlinks that you can use to navigate to the requirement and the linked MATLAB code range.
Note
To include requirement comments in generated code, you must have MATLAB Coder™ and Embedded Coder®.
Include Requirement Comments in Generated Code
If your MATLAB code contains links to requirements, you can include requirement comments
when you generate code by using the MATLAB Coder app or the codegen
(MATLAB Coder) function. For more information, see Generate C Code by Using the MATLAB Coder App (MATLAB Coder) and
Generate C Code at the Command Line (MATLAB Coder).
Include Requirement Comments by Using the MATLAB Coder App
To include requirement comments in generated code by using the MATLAB Coder (MATLAB Coder) app:
Open the MATLAB Coder app. In the Apps tab, under Code Generation, click MATLAB Coder. Alternatively, at the MATLAB command prompt, enter
coder
(MATLAB Coder).In the Generate code for function field, enter the name of your MATLAB function, then click Next. For more information, see Open the MATLAB Coder App and Select Source Files (MATLAB Coder).
Define the input types for the function, then click Next. For more information, see Define Input Types (MATLAB Coder).
Check for run-time issues by clicking Check for issues. If no issues are detected, click Next. For more information, see Check for Run-Time Issues (MATLAB Coder).
In the Build type menu, select one of these options:
Source Code
Static Library (.lib)
Dynamic Library (.dll)
Executable (.exe)
For more information, see Generate C Code (MATLAB Coder).
Note
You cannot include requirement comments in generated MEX functions.
To include requirement comments in the generated code, click More Settings. In the left pane, click Code Appearance. Under Comments, ensure that Include comments is selected, then select Requirement summaries as comments.
To generate a code generation report, in the left pane, click Debugging. Under Code Generation Report, ensure that Always create a report is selected.
After you configure any additional configuration parameters, click Close to close the code configuration parameters menu, then click Generate to generate the code. For more information, see Generate C Code (MATLAB Coder).
Include Requirement Comments Programmatically
Suppose that you want to generate code and include requirement comments for a
MATLAB function called myAdd
by using the codegen
(MATLAB Coder) function.
function y = myAdd(u,v) %#codegen y = u + v; end
To include requirement comments in the generated code:
Use
coder.config
(MATLAB Coder) with theecoder
flag set totrue
to create acoder.EmbeddedCodeConfig
(MATLAB Coder) object. You can use LIB, DLL, or EXE build types.cfg = coder.config("lib","ecoder",true);
Set the
ReqsInCode (MATLAB Coder)
property of thecoder.EmbeddedCodeConfig
object totrue
.cfg.ReqsInCode = true;
Set any additional code configuration parameters by modifying the properties of the
coder.EmbeddedCodeConfig
object. For more information, see Generate C Code at the Command Line (MATLAB Coder).Define function input data types and sizes with
coder.typeof
(MATLAB Coder). For more information, see Defining Input Types (MATLAB Coder).utype = coder.typeof(1); vtype = coder.typeof(1);
Generate the code by using
codegen
(MATLAB Coder). Use these flags as input arguments:-config
to specify the code configuration object to use during code generation-args
to specify the function input types and sizes-launchreport
to generate and launch the code generation report
codegen myAdd -config cfg -args {utype,vtype} -launchreport
View Comments in Generated Code
You can view the requirement comments by opening the generated entry-point C file, which has the same name as the MATLAB entry-point function. Each comment corresponds to a requirement link. The comment includes the:
Full file path to the MATLAB function
ID that represents the linked code range
Lines of MATLAB code that are linked to the requirement
Requirement summary
If multiple requirements are linked to the same code range, the comment lists the requirement summaries as a numbered list under the information about the linked code range.
For example, suppose you include requirement comments when you generate code from the
myAdd
function. The generated myAdd.c
entry-point file contains comments that correspond to the requirement
links:
double myAdd(double u, double v) { /* Requirements for MATLAB Code: '<C:\Users\jdoe\MATLAB\myAdd.m>|738609.742.1' * Line 1: * 1. Input u * 2. Input v * 3. Output y */ /* Requirements for MATLAB Code: '<C:\Users\jdoe\MATLAB\myAdd.m>|738609.742.3' * Line 2: * 1. Add u and v */ return u + v; }
Navigate to Requirements from Code Generation Report
MATLAB Coder code generation reports allow you to view the generated C/C++ code and trace the generated code to MATLAB source code. For more information, see Code Generation Reports (MATLAB Coder).
When you view the generated entry-point C file in a MATLAB Coder code generation report, the requirement comments are hyperlinks that you can use to navigate to the linked requirements in the Requirements Editor and the linked MATLAB code range in the MATLAB Editor.
Alternatively, you can trace between the generated code and the MATLAB source code without leaving the code generation report by using the Trace Code button. For more information, see Interactively Trace Between MATLAB Code and Generated C/C++ Code (Embedded Coder).
See Also
codegen
(MATLAB Coder) | coder.EmbeddedCodeConfig
(MATLAB Coder)