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 - matlabcoder.
- In the Create MATLAB Coder Project dialog, enter the project name and folder. Click OK. 
- In the MATLAB Coder pane, in the Input section, click Add Entry Points. In the Entry Points pane, enter the name of your MATLAB function. 
- Define the input types for the function by selecting an option from the Automatically Define Input Types drop-down menu. Then, click Run. For more information, see Define Input Types (MATLAB Coder). 
- In the MATLAB Coder tab, click Build type and set it to one of these options: - Source Code 
- Static Library (.lib) 
- Dynamic Library (.dll) 
- Executable (.exe) 
 - For more information, see Generate C Source Code (MATLAB Coder). - Note - You cannot include requirement comments in generated MEX functions. 
- To include requirement comments in the generated code, click Settings > View all settings. In the left pane of the Standalone Code Generation Settings dialog, click Code Appearance. Under Comments, select Include comments and select Requirement summaries as comments.  
- To generate a code generation report, in the left pane, click Debugging. Under Code Generation Report, select Always create a report. 
- After you configure any additional configuration parameters, close the Standalone Code Generation Settings menu, then click Generate Code. For more information, see Generate C Source 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 the- ecoderflag set to- trueto create a- coder.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 the- coder.EmbeddedCodeConfigobject to- true.- cfg.ReqsInCode = true; 
- Set any additional code configuration parameters by modifying the properties of the - coder.EmbeddedCodeConfigobject. 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:- -configto specify the code configuration object to use during code generation
- -argsto specify the function input types and sizes
- -launchreportto 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)