Main Content

Track Changes to MATLAB Code Using Requirements Editor

This example uses the ShortestPath project to show how to make changes to MATLAB® files and view the change issues against the requirements linked to the piece of MATLAB® code.

Open the ShortestPath project.

openProject("ShortestPath");

The project contains:

  • Requirement sets for functional (shortest_path_func_reqs.slreqx) and test requirements (shortest_path_tests_reqs.slreqx).

  • A MATLAB® algorithm (shortest_path.m)

  • MATLAB® unit tests (graph_unit_tests.m)

  • Links from MATLAB® code lines to requirements, and.slmx files stored in the src and tests folders.

Open MATLAB® Function

The shortest_path function tests the validity of the function inputs and then uses the Djikstra algorithm to calculate the number of edges in the shortest path between two nodes on a graph. Open the function.

open("shortest_path.m");

Open Requirement Files

Open the requirement sets in the Requirements Editor.

The shortest_path_func_reqs requirement set captures the functional behavior of the shortest_path function. Open the functional specification requirements file.

slreq.open("shortest_path_func_reqs.slreqx");

The shortest_path_tests_reqs requirement set contains test requirements that describe the functional behavior to be tested by a test case. The test requirements are derived from the functional requirements. There are test requirements for the nominal behavior and for the invalid conditions. Open the test specification requirements file.

slreq.open("shortest_path_tests_reqs.slreqx");

Make Changes to MATLAB® File

For the purpose of this example, change the value of the error code returned by the shortest_path.m function from -99 to -44 whenever the input start index or the end index is greater than the number of nodes in the graph. In the shortest_path.m file, on line number 37, change the value of ErrorCode from -99 to -44.

As you have changed the source, you also need to change the tests. Open graph_unit_tests.m file using this command.

open("graph_unit_tests.m")

In lines 16, 32, and 113, change the expected output value from -99 to -44 to match the source logic. Save the shortest_path.m and graph_unit_tests.m files.

Detect Change Issues

Click Refresh on the Requirements Editor toolstrip. The linked requirements now show a new icon indicating that an incoming link to that requirement has a change issue.

See Change in Link Details for Requirement

Navigate to the link in the Links pane to see that the Requirements Editor has detected a change issue from the MATLAB® code that you changed.

Select requirement 2.2.1 in shortest_path_tests_reqs.slreqx and click the red symbol in the Links section. It will navigate you to the code section of graph_unit_test.m and highlight it.change_matlab_code.png

See Change in Links View for Changed Link

From Requirements pane, select Show Links to navigate from the requirements view to the links view. Click the highlighted links to see the changed source along with the updated revision information under the Change Information section.

Run Tests

Optionally, you can run tests to confirm that the changed code is correct. Right-click the shortest_path_tests_reqs.slreqx requirement set and click Run Tests. The result shows that all tests for this change have passed.

Clear Change Issue

From Requirements pane, make sure that Show Links is selected in order to clear the change issue for the requirement sets. Select shortest_path_tests_reqs.slmx and click Clear issue under Change Information section. This clears the change issue in the source code of the shortest_path.m file.

Since there are multiple links from the graph_unit_tests.slmx test file, you can right-click the graph_unit_tests.slmx link set and select Clear all change issues to clear all the change issues at once. Save both linkset files after clearing the change issue.

Click Refresh in the Requirements Editor to see that the warning icon on the requirements does not show anymore.

Related Topics