Documentation

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';
open_system(model);
shh = get(0,'ShowHiddenHandles');
set(0,'ShowHiddenHandles','On');
hscope = findobj(0,'Type','Figure','Tag','SIMULINK_SIMSCOPE_FIGURE');
close(hscope);
set(0,'ShowHiddenHandles',shh);

Collect Metrics

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

metric_engine = slmetric.Engine();
setAnalysisRoot(metric_engine,'Root','vdp','RootType','Model');
execute(metric_engine);

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',...
'mathworks.metrics.SubSystemCount'});

Display and Store 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.

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;
        end
    else
        disp(['No results for:',res_col(n).MetricID]);
    end
    disp(' ');
end

Here are the results.

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 a Spreadsheet

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';
T=table(metricData);
writetable(T,filename);

Export Results to an XML File

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.

filename='MyMetricResults.xml';
exportMetrics(metric_engine,filename)

Close the model vdp.

bdclose(model);

Limitations

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

Was this topic helpful?