Collect Model Metrics Programmatically

This example shows how to use the model metric API to programmatically collect subsystem and block count metrics for a model. After collecting metrics for the model, you can access the results and export them to a file.

Example Model

Open model vdp.

model = 'vdp';

Collect Metrics

To collect metric data on a model, create a metric engine object and call execute.

metric_engine = slmetric.Engine();
Updating Model Advisor cache...
Model Advisor cache updated. For new customizations, to update the cache, use the Advisor.Manager.refresh_customizations method.

Access Results

Using the getMetrics method, specify the metrics you want to collect. For this example, specify the block count and subsystem count metrics for the vdp model. getMetrics returns an array of slmetric.metric.ResultCollection objects.

res_col = getMetrics(metric_engine,{'mathworks.metrics.SimulinkBlockCount',...

Store and Display Results

Create cell array metricData to store the MetricID, ComponentPath, and Value for the metric results. The MetricID is the identifier for the metric, the ComponentPath is the path to component for which the metric is calculated, and the Value is the metric value. Write a loop to display the results.

metricData ={'MetricID','ComponentPath','Value'};
cnt = 1;
for n=1:length(res_col)
    if res_col(n).Status == 0
        results = res_col(n).Results;

        for m=1:length(results)
            disp(['MetricID: ',results(m).MetricID]);
            disp(['  ComponentPath: ',results(m).ComponentPath]);
            disp(['  Value: ',num2str(results(m).Value)]);
            metricData{cnt+1,1} = results(m).MetricID;
            metricData{cnt+1,2} = results(m).ComponentPath;
            metricData{cnt+1,3} = results(m).Value;
            cnt = cnt + 1;
        disp(['No results for:',res_col(n).MetricID]);
    disp(' ');
MetricID: mathworks.metrics.SimulinkBlockCount
  ComponentPath: vdp
  Value: 11
MetricID: mathworks.metrics.SimulinkBlockCount
  ComponentPath: vdp/More Info
  Value: 1
MetricID: mathworks.metrics.SubSystemCount
  ComponentPath: vdp
  Value: 1
MetricID: mathworks.metrics.SubSystemCount
  ComponentPath: vdp/More Info
  Value: 0

Export Results

To export the metricData results MetricID, ComponentPath, and Value to a spreadsheet, use writetable to write the contents of metricData to MySpreadsheet.xlsx.

filename = 'MySpreadsheet.xlsx';

To export the metric results to an XML file, use the exportMetrics method. For each metric result, the XML file includes the ComponentID, ComponentPath, MetricID, Value, AggregatedValue, and Measure.


Close the model vdp.



For one model, you cannot collect metric data into the same database file (that is, the Metrics.db file) on multiple platforms.

See Also

| |

Related Topics