Main Content

Create a Custom Model Metric for Nonvirtual Block Count

This example shows how to use the model metric API to create a custom model metric for counting nonvirtual blocks in a model. After creating the metric, you can collect data for the metric, access the results, and export the results.

Create Metric Class

To create a custom model metric, use the slmetric.metric.createNewMetricClass function to create a new metric class derived from the base class slmetric.metric.Metric. The slmetric.metric.createNewMetricClass function creates a file that contains a constructor and an empty metric algorithm method.

1. For this example, make sure that you are in a writeable folder and create a new metric class named nonvirtualblockcount.

className = 'nonvirtualblockcount';

2. Ensure that a custom metricID named nonvirtualblockcount is not already registered in the model metric repository.


3. Write the metric algorithm into the slmetric.metric.Metric method, algorithm. The algorithm calculates the metric data specified by the Advisor.component.Component class. The Advisor.component.Types class specifies the types of model objects for which you can calculate metric data. For this example, the file nonvirtualblockcount_orig.m contains the logic to create a metric that counts the nonvirtual blocks. Copy this file to the nonvirtualblockcount.m file.

copyfile nonvirtualblockcount_orig.m nonvirtualblockcount.m f

When creating a custom metric, you must set the following properties of the slmetric.metric.Metric class:

  • ID: Unique metric identifier that retrieves the new metric data.

  • Name: Name of the metric algorithm.

  • ComponentScope: Model components for which the metric is calculated.

  • CompileContext: Compile mode for metric calculation. If your model requires model compilation, specify PostCompile. Collecting metric data for compiled models slows performance.

  • ResultCheckSumCoverage: Specify whether you want the metric data regenerated if source file and Version have not changed.

  • AggregationMode: How the metric algorithm aggregates metric data

Optionally, you can set these additional properties:

  • Description: Description of the metric.

  • Version: Metric version.

4. Now that your new model metric is defined in nonvirtualblockcount.m, you can register the new metric in the metric repository.

[id_metric,err_msg] = slmetric.metric.registerMetric(className);

Collect Metric Data

To collect metric data on models, use instances of slmetric.Engine. Using the getMetrics method, specify the metrics you want to collect. For this example, specify the nonvirtual block count metric for the sldemo_mdlref_conversion model.

1. Load the sldemo_mdlref_conversion model.

model = 'sldemo_mdlref_conversion';

2. Create a metric engine object and set the analysis root.

metric_engine = slmetric.Engine();

3. Collect metric data for the nonvirtual block count metric.

rc = getMetrics(metric_engine,id_metric);

Display and Export Results

To access the metrics for your model, use instance of slmetric.metric.Result. In this example, display the nonvirtual block count metrics for the sldemo_mdlref_conversion model. For each result, display the MetricID, ComponentPath, and Value.

for n=1:length(rc)
    if rc(n).Status == 0
        results = rc(n).Results;

        for m=1:length(results)
            disp(['MetricID: ',results(m).MetricID]);
            disp(['  ComponentPath: ', results(m).ComponentPath]);
            disp(['  Value: ', num2str(results(m).Value)]);
            disp(' ');
        disp(['No results for:',rc(n).MetricID]);
    disp(' ');
MetricID: nonvirtualblockcount
  ComponentPath: sldemo_mdlref_conversion
  Value: 7
MetricID: nonvirtualblockcount
  ComponentPath: sldemo_mdlref_conversion/Bus Counter
  Value: 8
MetricID: nonvirtualblockcount
  ComponentPath: sldemo_mdlref_conversion/More Info
  Value: 0
MetricID: nonvirtualblockcount
  ComponentPath: sldemo_mdlref_conversion/SubSystem1
  Value: 0

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.


For this example, unregister the nonvirtual block count metric.


Close the model.



Custom metric algorithms do not support the path property on component objects:

  • Linked Stateflow® charts

  • MATLAB Function blocks

Custom metric algorithms do not follow library links.

See Also

| | | | |

Related Topics