## Model Metric Data Aggregation

You can better understand the size, complexity, and readability of a model and its components by analyzing aggregated model metric data. Aggregated metric data is available in the `AggregatedValue` and `AggregatedMeasures` properties of an `slmetric.metric.Result` object. The `AggregatedValue` property aggregates the metric scalar values. The `AggregatedMeasures` property aggregates the metric measures (that is, the detailed information about the metric values).

### How Model Metric Aggregation Works

The implementation of a model metric defines how a metric aggregates data across a component hierarchy. For MathWorks model metrics, the `slmetric.metric.Metric` class defines model metric aggregation. This class includes the `AggregationMode` property, which has these options:

• `Sum`: Returns the sum of the `Value` property and the `Value` properties of its children components across the component hierarchy. Returns the sum of the `Meaures` property and the `Measures` properties of its children components across the component hierarchy.

• `Max`: Returns the maximum of the `Value` property and the `Value` properties of its children components across the component hierarchy. Returns the maximum of the `Measures` property and the `Measures` properties of its children components across the component hierarchy.

• `None`: No aggregation of metric values.

You can find descriptions of MathWorks model metrics and their `AggregationMode` property setting in Model Metrics. For custom metrics, as part of the `algorithm` method, you can define how the metric aggregates data. For more information, see Create a Custom Model Metric for Nonvirtual Block Count.

This diagram shows how the software aggregates metric data across the components of a model hierarchy. The parent model is at the top of the hierarchy. The components can be the following:

• Model

• Subsystem block

• Chart

• MATLAB function block

• Protected model In the diagram, the `AggregationMode` is `Sum` and the model and components in the hierarchy each have a `Value` and an `AggregatedValue`. The `AggregatedValue` for a parent model or component is the sum of its `Value` and the `AggregatedValue` of each direct child component. For example, in this diagram, the `AggregatedValue` of the parent model is `75`. The `AggregatedValue` of the parent model is calculated as the sum of the `Value` of the parent model, `6`, plus the `AggregatedValue` of each direct child component, `33`, `17`, and `19`.

### Access Aggregated Metric Data

This example shows how to collect metric data programmatically in the metric engine, and then access aggregated metric data.

1. Load the `sldemo_auto_climatecontrol` model.

`openExample('sldemo_auto_climatecontrol')`
2. Create an `slmetric.Engine` object and set the analysis root.

```metric_engine = slmetric.Engine(); setAnalysisRoot(metric_engine,'Root',... 'sldemo_auto_climatecontrol','RootType','Model');```

3. Collect data for the Input output model metric.

`execute(metric_engine,'mathworks.metrics.IOCount');`
4. Get the model metric data that returns an array of `slmetric.metric.ResultCollection` objects, `res_col`. Specify the input argument for `AggregationDepth`.

```res_col = getMetrics(metric_engine,'mathworks.metrics.IOCount',... 'AggregationDepth','All');```

The `AggregationDepth` input argument has two options: `All` and `None`. If you do not want the `getMetrics` method to aggregate measures and values, specify `None`.

5. Display the results.

```metricData ={'MetricID','ComponentPath','Value',... 'AggregatedValue','Measures','AggregatedMeasures'}; 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)]); disp([' Aggregated Value: ',num2str(results(m).AggregatedValue)]); disp([' Measures: ',num2str(results(m).Measures)]); disp([' Aggregated Measures: ',... num2str(results(m).AggregatedMeasures)]); metricData{cnt+1,1} = results(m).MetricID; metricData{cnt+1,2} = results(m).ComponentPath; metricData{cnt+1,3} = results(m).Value; tdmetricData{cnt+1,4} = results(m).Measures; metricData{cnt+1,5} = results(m).AggregatedMeasures; cnt = cnt + 1; end else disp(['No results for:',res_col(n).MetricID]); end disp(' '); end```

Here are the results:

```MetricID: mathworks.metrics.IOCount ComponentPath: sldemo_auto_climatecontrol Value: 0 Aggregated Value: 9 Measures: 0 0 0 0 Aggregated Measures: 5 4 0 0 MetricID: mathworks.metrics.IOCount ComponentPath: sldemo_auto_climatecontrol/AC Control Value: 6 Aggregated Value: 6 Measures: 5 1 0 0 Aggregated Measures: 5 1 0 0 MetricID: mathworks.metrics.IOCount ComponentPath: sldemo_auto_climatecontrol/External Temperature in Celsius Value: 1 Aggregated Value: 1 Measures: 0 1 0 0 Aggregated Measures: 0 1 0 0 MetricID: mathworks.metrics.IOCount ComponentPath: sldemo_auto_climatecontrol/Heat from occupants Value: 1 Aggregated Value: 1 Measures: 0 1 0 0 Aggregated Measures: 0 1 0 0 MetricID: mathworks.metrics.IOCount ComponentPath: sldemo_auto_climatecontrol/Heater Control Value: 8 Aggregated Value: 8 Measures: 5 3 0 0 Aggregated Measures: 5 3 0 0 MetricID: mathworks.metrics.IOCount ComponentPath: sldemo_auto_climatecontrol/Interior Dynamics Value: 3 Aggregated Value: 3 Measures: 2 1 0 0 Aggregated Measures: 2 1 0 0 MetricID: mathworks.metrics.IOCount ComponentPath: sldemo_auto_climatecontrol/More Info Value: 0 Aggregated Value: 0 Measures: 0 0 0 0 Aggregated Measures: 0 0 0 0 MetricID: mathworks.metrics.IOCount ComponentPath: sldemo_auto_climatecontrol/Subsystem Value: 2 Aggregated Value: 2 Measures: 1 1 0 0 Aggregated Measures: 1 1 0 0 MetricID: mathworks.metrics.IOCount ComponentPath: sldemo_auto_climatecontrol/Subsystem1 Value: 2 Aggregated Value: 2 Measures: 1 1 0 0 Aggregated Measures: 1 1 0 0 MetricID: mathworks.metrics.IOCount ComponentPath: sldemo_auto_climatecontrol/Temperature Control Chart Value: 9 Aggregated Value: 9 Measures: 5 4 0 0 Aggregated Measures: 5 4 0 0 MetricID: mathworks.metrics.IOCount ComponentPath: sldemo_auto_climatecontrol/User Setpoint in Celsius Value: 1 Aggregated Value: 1 Measures: 0 1 0 0 Aggregated Measures: 0 1 0 0```

For the Input output metric, the `AggregationMode` is `Max`. For each component, the `AggregatedValue` and `AggregatedMeasures` properties are the maximum number of inputs and outputs of itself and its children components. For example, for `sldemo_auto_climatecontrol`, the `AggregatedValue` property is `9`, which is the `sldemo_auto_climatecontrol/Temperature Control Chart` component value.