Link Requirements to MATLAB Code
Requirements traceability refers to the relationship between requirements and design artifacts, test artifacts, and other requirements. Traceability makes it possible to follow requirements to the artifacts that they are related to and from artifacts to the requirements. Requirements traceability justifies the existence of components in the design and individual tests in a test artifact.
In Requirements Toolbox™, you establish traceability by creating links. Links point from the source item to the destination item and allow you to navigate between requirements in the Requirements Editor and their linked items, such as lines of MATLAB® code, Simulink® blocks, and MATLAB and Simulink tests.
In this step of the tutorial, you create links from lines of MATLAB code in the myAdd
function to the functional requirements that you authored in the previous step.
Open Project Files
Open the MyAdd
project.
openProject("MyAdd");
Open the myAddReqs
requirement set in the Requirements Editor. In the documents
folder of the project, right-click myAddReqs.slreqx
and select Open.
In MATLAB, open the myAdd
function.
open myAdd
The myAdd
function implements the functional requirements described in myAddReqs
. The function takes two inputs, checks that they are finite and that they are real or complex numbers, adds the inputs together, and outputs the result.
Use the function to add 1
and 2
together.
y = myAdd(1,2)
y = 3
Link Requirements to Code Lines
Establish traceability between the requirements and the design by creating links from lines of code in the myAdd
function to the functional requirements in the myAddReqs
requirement set.
Create a link between the function and the requirement that has the summary Take two inputs
. In the MATLAB Editor, select the function definition line for the myAdd
function.
In the Requirements Editor, select the requirement that has the summary Take two inputs
. Create the link by selecting Add Link > Link from Selection in MATLAB Editor.
Use the Add Link button to create these additional links between lines of code in myAdd
and requirements in myAddReqs
:
Line 8 in
myAdd
and the requirement that has the summaryAdd the inputs
The function definition line (line 1) in
myAdd
and the requirement that has the summaryOutput the result
Lines 5 and 6 in
myAdd
and the requirement that has the summaryInputs should be finite
View and Save Links
View the links that you created by clicking Show Links in the Requirements Editor.
When you create links between requirements and design or test items, Requirements Toolbox usually assigns the design or test item as the link source and the requirement as the destination. The name of the link set combines the name of the source artifact with the file extension of the source artifact, separated by a tilde. In this case, the base name of the source artifact is myAdd
and the file extension is .m
, so the link set name is myAdd~m
, followed by the .slmx
link set file extension. Requirements Toolbox stores the link set in the same folder as the source artifact.
The asterisk in the link set name indicates that the link set is unsaved. Save the link set by selecting it and clicking Save.
Navigate Between Source and Destination
Navigate between the requirement that has the summary Take two inputs
and the linked code line in the myAdd
function.
In the Requirements Editor, click Show Requirements. Select the requirement that has the summary Take two inputs
. Navigate to the myAdd
function definition line by clicking the link in the right pane under Links.
The code file opens in the MATLAB Editor with the linked code line selected. Enable requirements highlighting by right-clicking in the MATLAB Editor and selecting Requirements > Enable Requirements Highlighting. When you enable requirements highlighting, the linked code lines are highlighted in yellow.
Navigate from the function definition line to the requirement that has the summary Take two inputs
by right-clicking the code line and selecting Requirements > 1. Take two inputs.