Main Content

Collect Model Maintainability Metrics Programmatically

This example shows how to collect the metric data used in the Model Maintainability Dashboard, generate a report, and use the report to assess the model design artifacts.

If a model in your project is large, complex, or difficult to read, the design can be difficult to test and maintain. Use the metric results to determine if you want to refactor your design to identify smaller testable units, or restructure a model to improve readability.

Collect and Access Metric Results

Open Project

Open a project containing models that you want to analyze. For this example, in the MATLAB® Command Window, enter:

openProject("cc_CruiseControl");

Collect Metric Results

Create a metric.Engine object. You can use the metric.Engine object to collect the metric data for the current project.

metric_engine = metric.Engine();

Create an array of the metric identifiers for model maintainability.

maintainabilityMetrics = getAvailableMetricIds(metric_engine,...
App="DashboardApp",...
Dashboard="ModelMaintainability");

You can collect results for one unit at a time or for each unit in the project.

For this example, suppose you want to collect results for the unit cc_ControlMode. Create an array that identifies the path to the model file in the project and the name of the model.

unit = [fullfile(pwd,"models","cc_ControlMode.slx"),"cc_ControlMode"];

Use the execute function to collect the maintainability metric results for the unit cc_ControlMode.

execute(metric_engine,maintainabilityMetrics,ArtifactScope=unit);

Access and View Metric Results

Access the metric results by using the getMetrics function. For this example, store the results for the metric slcomp.SimulinkSignalLines and for the unit cc_ControlMode.

results_SignalLines = getMetrics(metric_engine,"slcomp.SimulinkSignalLines",...
ArtifactScope=unit);

The model maintainability metric slcomp.SimulinkSignalLines determines the number of Simulink® signal lines in each layer of a model. For more information on the model maintainability metrics, see Model Maintainability Metrics.

The getMetrics function returns a metric.Result object containing the metric results for the specified metric.

Use the disp function to display the model layer and number of signal lines, which are in the Artifacts and Value properties of the metric.Result object. The results may appear in a different order on your machine.

for n=1:length(results_SignalLines)

    disp([' Model Layer: ', results_SignalLines(n).Artifacts.Name])
    disp(['Signal Lines: ', num2str(results_SignalLines(n).Value)])
    disp(newline);

end
 Model Layer: cc_ControlMode
Signal Lines: 14
 Model Layer: Control_Mode_StateMachine
Signal Lines: 60
 Model Layer: Transitions_From_THROTTLE_OVERRIDE
Signal Lines: 14
 Model Layer: cc_ControlMode_DisableTransition
Signal Lines: 10
 Model Layer: cc_ControlMode_DeactivateTransition
Signal Lines: 10
 Model Layer: Target_Speed_Calculator
Signal Lines: 18
 Model Layer: check_speed_range
Signal Lines: 6
 Model Layer: Switch Case Action
Subsystem
Signal Lines: 1
 Model Layer: Transitions_From_ACTIVE
Signal Lines: 14
 Model Layer: Switch Case Action
Subsystem1
Signal Lines: 1
 Model Layer: wasActiveOnce
Signal Lines: 12
 Model Layer: Transitions_From_DISABLED
Signal Lines: 11
 Model Layer: Transitions_From_ENABLED
Signal Lines: 20
 Model Layer: Switch Case Action
Subsystem
Signal Lines: 23
 Model Layer: Switch Case Action
Subsystem4
Signal Lines: 1
 Model Layer: isFirstStep
Signal Lines: 2
 Model Layer: Switch Case Action
Subsystem3
Signal Lines: 1
 Model Layer: Switch Case Action
Subsystem2
Signal Lines: 1
 Model Layer: Switch Case Action
Subsystem2
Signal Lines: 1
 Model Layer: Switch Case Action
Subsystem1
Signal Lines: 1

The code output shows that the model layer Control_Mode_StateMachine contains 60 Simulink signal lines. Based on these results, consider restructuring that model layer to improve readability.

Generate Report

Generate a report file containing the model maintainability results collected in metric_engine. By default, the report file type is a PDF. For this example, specify the report file type as HTML.

generateReport(metric_engine,...
App="DashboardApp",...
Dashboard="ModelMaintainability",...
Type="html-file");

Save the metric results in a report file to access them without opening the project or dashboard.

Alternatively, you can open the Model Maintainability Dashboard to see the results and explore the artifacts. To programmatically access the Model Maintainability Dashboard, enter:

modelDesignDashboard

See Also

Related Topics