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.
Open model vdp.
model = 'vdp';
open_system(model);
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);
Updating Model Advisor cache... Model Advisor cache updated. For new customizations, to update the cache, use the Advisor.Manager.refresh_customizations method.
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'});
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; end else disp(['No results for:',res_col(n).MetricID]); end disp(' '); end
MetricID: mathworks.metrics.SimulinkBlockCount
ComponentPath: vdp
Value: 13
MetricID: mathworks.metrics.SimulinkBlockCount
ComponentPath: vdp/More Info
Value: 1
MetricID: mathworks.metrics.SimulinkBlockCount
ComponentPath: vdp/More Info/Model Info
Value: 1
MetricID: mathworks.metrics.SimulinkBlockCount
ComponentPath: vdp/More Info/Model Info/EmptySubsystem
Value: 0
MetricID: mathworks.metrics.SubSystemCount
ComponentPath: vdp
Value: 1
MetricID: mathworks.metrics.SubSystemCount
ComponentPath: vdp/More Info
Value: 0
MetricID: mathworks.metrics.SubSystemCount
ComponentPath: vdp/More Info/Model Info
Value: 1
MetricID: mathworks.metrics.SubSystemCount
ComponentPath: vdp/More Info/Model Info/EmptySubsystem
Value: 0
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);
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);
For one model, you cannot collect metric data into the same database file (that is, the Metrics.db
file) on multiple platforms.
slmetric.Engine
| slmetric.metric.Result
| slmetric.metric.ResultCollection