Contenido principal

Link MATLAB Code to Requirements

You can link requirements to MATLAB® code and plain-text external code, such as C code, by using the Requirements Editor or programmatically at the MATLAB command line. You can also create links to MATLAB code in MATLAB Function (Simulink) blocks. You can view, edit, and delete the links to code in the MATLAB Editor or Requirements Editor.

Create Links to MATLAB Code or Plain-Text External Code

You can create links to MATLAB code or plain-text external code programmatically or by using the Requirements Editor.

When you create links to code, Requirements Toolbox™ creates slreq.TextRange objects that correspond to the selected lines. These slreq.TextRange objects are referred to as line ranges.

To create links programmatically, create the slreq.TextRange object, then use the object as a link source when you create the link. When you create multiple slreq.TextRange objects in the same MATLAB code file or plain-text external code file, the line numbers for the slreq.TextRange objects cannot overlap.

Create Links by Using the Requirements Editor

To create selection-based links to code by using the Requirements Editor:

  1. In the MATLAB Editor, open the MATLAB code file or plain-text external code file.

    Note

    You cannot create links to MATLAB code in MLX files.

  2. Select the lines of code that you want to link.

  3. In the Requirements Editor, load the requirement set that you want to link.

  4. Select the requirement to link.

  5. In the Links section, click Add Link > Link from Selection in MATLAB Editor.

    Alternatively, in the MATLAB Editor, right-click the selected code range and select Requirements > Link to Selection in Requirements Browser.

Create Links Programmatically

Suppose that you want to programmatically create links to a MATLAB function called myAdd.

function y = myAdd(u,v)
y = u + v;
end
You want to link the function to these requirements:

Requirements for the myAdd function, including requirements for the inputs, outputs, and function behavior.

To create links at the MATLAB command line:

  1. Use slreq.createTextRange to create an slreq.TextRange object that represents the lines of code that you want to link to.

    lr = slreq.createTextRange("myAdd.m",2);

  2. Use slreq.find, find, or slreq.getCurrentObject to get a handle to the requirement that you want to link.

    req = slreq.find(Type="Requirement",Summary="Add u and v");

  3. Use slreq.createLink to create the link.

    myLink = slreq.createLink(lr,req);

Create Links to MATLAB Function Blocks

Use the MATLAB Function Block Editor (Simulink) to create links to lines of code in MATLAB Function blocks:

  1. In the Requirements Editor, load the requirement set that you want to link to.

  2. Select the requirement to link.

  3. In the Simulink® model, open the MATLAB Function block.

  4. Select the lines of code that you want to link.

  5. Right-click the selected code range and select Requirements > Link to Selection in Requirements Browser.

Note

Requirements linked to MATLAB code lines inside a MATLAB Function block appear in HTML requirements traceability reports, but do not appear the Simulink Report Generator™ Web View. See Create and Use Web Views of Models (Simulink Report Generator).

Create Links to Requirements in External Documents

To create links from MATLAB code to requirements in external documents:

  1. Select a requirement in one of these external documents:

    • Microsoft® Word

    • Microsoft Excel®

    • IBM® DOORS®

    • IBM DOORS Next

  2. In the MATLAB Editor, open the MATLAB code file or plain-text external code file.

    Note

    You cannot create links to MATLAB code in MLX files.

  3. Select the lines of code that you want to link.

  4. Right-click the selected code range and select Requirements. Depending on the type of your requirements document, select one of these options:

    • Link to Selection in Word

    • Link to Selection in Excel

    • Link to Selection in DOORS

    • Link to Selected Item(s) in DOORS Next

  5. Save the changes. For more information, see Save Links.

For more information about configuring Requirements Toolbox to work with these third-party products, see Configure Requirements Toolbox for Interaction with Microsoft Office and IBM DOORS and Configure IBM DOORS Next for Integration with Requirements Toolbox.

View and Edit Links and Line Ranges

To view the links and line ranges for the current file in MATLAB Editor, open the Requirements Traceability panel by right-clicking in the editor and selecting Requirements > Open Requirements Traceability Panel (since R2025a). Line ranges in the current file have the line range icon and link destinations have the link destination icon .

This image shows the Requirements Traceability panel. The panel contains 3 line ranges and 4 links.

You can also view linked code ranges in the MATLAB Editor by enabling requirements highlighting. Right-click in the editor and select Requirements > Enable Requirements Highlighting.

To edit links, use the Requirements Editor. For more information, see View and Edit Links.

Edit Line Ranges in the MATLAB Editor

To edit the line numbers for a line range in the MATLAB Editor:

  1. Open the Requirements Traceability panel and select an item that has the line range icon . The editor selects the code in the line range (since R2025a).

  2. Unselect the code in the line range by clicking in the editor. Then, right-click in the line range and select Requirements > Adjust line range. The Adjust range dialog box opens.

  3. In the Adjust range dialog box, use the + and - buttons to change the first and last lines of the line range. Alternatively, enter the line number in the field.

    Adjust range dialog box showing lines 1 and 2 as the first and last lines for the line range.

  4. Click OK.

  5. Save the changes. For more information, see Save Links.

Alternatively to using the Requirements Traceability panel to identify line ranges, you can highlight the linked line ranges by right-clicking in the editor and selecting Requirements > Enable Requirements Highlighting.

Edit Line Ranges Programmatically

To edit the lines for an slreq.TextRange object at the MATLAB command line:

  1. Open the MATLAB code file or plain-text external code file in the MATLAB Editor.

    open("myAdd.m");

  2. Get the existing slreq.TextRange object in the MATLAB code file by passing the file name and the line numbers for the start and end of the line range to the slreq.getTextRange function.

    lr = slreq.getTextRange("myAdd.m",[1 2]);

  3. Modify the line range by using the setLineRange method.

    setLineRange(lr,1);

  4. Save the changes.

    myLinkSet = slreq.find(Type="LinkSet",Artifact=lr.Artifact);
    save(myLinkSet);

The MATLAB Editor updates the code range highlighting. Alternatively, you can confirm the changes by using show, getText, or getLineRange.

Delete Links and Unused Line Ranges

You can delete links to MATLAB code in the MATLAB Editor, the Requirements Editor, the Requirements Traceability panel, or at the MATLAB command line.

If you delete links to code ranges in the MATLAB Editor, you can delete the unused line ranges in the editor, in the Requirements Traceability panel, or at the MATLAB command line.

Delete Links

To delete individual links in a loaded link set, use the Requirements Editor. For more information, see Delete Links and Link Sets.

To delete individual links in the current file in the MATLAB Editor, use the Requirements Traceability panel. For more information, see Delete Links.

To delete all incoming and outgoing links to a line range in the MATLAB Editor, right-click a highlighted code range and select Requirements > Delete All Links.

To programmatically delete links, get a handle to an slreq.Link object and use the remove function. Alternatively, open a file that contains incoming or outgoing links in the MATLAB Editor. Then, get a handle to an slreq.TextRange object by using the slreq.getTextRange function. Delete the links to the object by using the deleteLinks function.

Delete Line Ranges

To delete line ranges in the current file in the MATLAB Editor, use the Requirements Traceability panel. For more information, see Delete Links.

To programmatically delete line ranges, open a file that contains incoming or outgoing links in the MATLAB Editor. Then, get an slreq.TextRange object by using the slreq.getTextRange function. Delete the code range by using the remove function.

Repair Links

If you delete a line range that has outgoing links or if the software cannot locate a line range in a MATLAB code or plain-text file, links to the line range become unresolved. You can repair the unresolved links, or delete them if they are no longer needed. For more information, see Resolve Links.

To view unresolved links, in the Requirements Editor, click Show Links. Unresolved links display the unresolved link icon .

To view orphan links, links that have invalid sources, use the Requirements Traceability panel. The panel displays the orphan links in red text.

Save Links

To save the changes to a link set when you create, edit, delete, or repair links or line ranges, use one of these approaches:

  • In the MATLAB Editor, right-click and select Requirements > Save Links.

  • In the Requirements Editor, click Show Links. Select the link set, and click Save.

  • At the MATLAB command line, use save.

See Also

Apps

Classes

Functions

Topics