This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

slmetric.dashboard.CustomWidget class

Package: slmetric.dashboard

Object for holding custom Metrics Dashboard widgets


For custom or shipped metrics, use the slmetric.dashboard.CustomWidget object to visualize metric data in the Metrics Dashboard. Choose a single value, radial gauge, bar chart, or distribution heat map approach.


For slmetric.dashboard.Layout, slmetric.dashboard.Container, or slmetric.dashboard.Group objects, use the addWidget or removeWidget methods to add or remove slmetric.dashboard.CustomWidget objects from the Metrics Dashboard. Use slmetric.dashboard.CustomWidget methods to specify the widget size.


expand all

Type of slmetric.dashboard.CustomWidget object that you want to add, remove, or modify in the Metrics Dashboard. This property is read/write. Choose from these widget types:

Data Types: char

Add labels to custom widget. This property is only for the BarChart VisualizationType property, so you can add apply labels to each individual bar. This property is read/write.

Data Types: char

Specify a title for the custom widget. For a radial gauge, there is a 16 character limit. This property is read/write.

Data Types: char

Type of slmetric.dashboard.CustomWidget object. This property is read-only.

Data Types: char


getHeight Obtain height of Metrics Dashboard custom widget
getMetricIDs Obtain metric identifier for custom Metrics Dashboard widget
getPosition Obtain custom widget position within Metrics Dashboard
getSeparatorsDetermine whether there are lines on sides of Metrics Dashboard custom widget
getWidths Obtain widths of Metrics Dashboard custom widget
setHeight Specify height of Metrics Dashboard custom widget
setMetricIDs Set metric identifier for custom Metrics Dashboard widget
setPosition Set custom widget position within Metrics Dashboard
setSeparators Specify lines on Metrics Dashboard custom widget sides
setWidths Specify multiples widths for Metrics Dashboard custom widget


collapse all

Create a custom metric that counts nonvirtual blocks. Specify a widget to display this metric on the Metrics Dashboard. Add it to the Size Group.

Create a custom metric class.

className = 'nonvirtualblockcount';

Create the nonvirtual block count metric by adding this code to the nonvirtualblockcount.m file.

classdef nonvirtualblockcount < slmetric.metric.Metric
    %nonvirtualblockcount calculates number of nonvirtual blocks per level.
    % BusCreator, BusSelector and BusAssign are treated as nonvirtual.
        VirtualBlockTypes = {'Demux','From','Goto','Ground', ...
            'GotoTagVisiblity','Mux','SignalSpecification', ...
    function this = nonvirtualblockcount()
        this.ID = 'nonvirtualblockcount';
        this.Name = 'Nonvirtual Block Count';
        this.Version = 1;
        this.CompileContext = 'None';
        this.Description = 'Algorithm that counts nonvirtual blocks per level.';
        this.AggregatedValueName = 'Nonvirtual Blocks (incl. Descendants)'
        this.ValueName = 'Nonvirtual Blocks'
        this.ComponentScope = [Advisor.component.Types.Model, ...
        this.AggregationMode = slmetric.AggregationMode.Sum;
	    this.AggregateComponentDetails = true;
        this.ResultChecksumCoverage = true;
        this.SupportsResultDetails = true;

    function res = algorithm(this, component)
        % create a result object for this component
        res = slmetric.metric.Result();	

        % set the component and metric ID
        res.ComponentID = component.ID;
        res.MetricID = this.ID;
        % Practice
        D1=slmetric.metric.ResultDetail('identifier 1','Name 1');
        D2=slmetric.metric.ResultDetail('identifier 2','Name 2');

        % use find_system to get blocks inside this component
        blocks = find_system(getPath(component), ...
            'SearchDepth', 1, ...
            'Type', 'Block');

        isNonVirtual = true(size(blocks));

        for n=1:length(blocks)
            blockType = get_param(blocks{n}, 'BlockType');

            if any(strcmp(this.VirtualBlockTypes, blockType))
                isNonVirtual(n) = false;
                switch blockType
                    case 'SubSystem'
                        % Virtual unless the block is conditionally executed
                        % or the Treat as atomic unit check box is selected.
                        if strcmp(get_param(blocks{n}, 'IsSubSystemVirtual'), ...
                            isNonVirtual(n) = false;
                    case 'Outport'
                        % Outport: Virtual when the block resides within
                        % SubSystem block (conditional or not), and 
                        % does not reside in the root (top-level) Simulink window.
                        if component.Type ~= Advisor.component.Types.Model
                            isNonVirtual(n) = false;
                    case 'Selector'
                        % Virtual only when Number of input dimensions 
                        % specifies 1 and Index Option specifies Select 
                        % all, Index vector (dialog), or Starting index (dialog).
                        nod = get_param(blocks{n}, 'NumberOfDimensions');
                        ios = get_param(blocks{n}, 'IndexOptionArray');

                        ios_settings = {'Assign all', 'Index vector (dialog)', ...
                            'Starting index (dialog)'};

                        if nod == 1 && any(strcmp(ios_settings, ios))
                            isNonVirtual(n) = false;
                    case 'Trigger'
                        % Virtual when the output port is not present.
                        if strcmp(get_param(blocks{n}, 'ShowOutputPort'), 'off')
                            isNonVirtual(n) = false;
                    case 'Enable'
                        % Virtual unless connected directly to an Outport block.
                        isNonVirtual(n) = false;

                        if strcmp(get_param(blocks{n}, 'ShowOutputPort'), 'on')
                            pc = get_param(blocks{n}, 'PortConnectivity');

                            if ~isempty(pc.DstBlock) && ...
                                    strcmp(get_param(pc.DstBlock, 'BlockType'), ...
                                isNonVirtual(n) = true;

        blocks = blocks(isNonVirtual);

        res.Value = length(blocks);

Register the new metric in the metric repository.

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

To begin, open the default configuration for the Metrics Dashboard layout.

conf =;

Obtain the slmetric.dashboard.Layout object from the slmetric.dashboard.Configuration object.

layout = getDashboardLayout(conf);

Obtain widget objects that are in the layout object.


Remove the widget that represents the Simulink block count metric.

sizeGroup = layoutWidget(2); 
sizeGroupWidgets = sizeGroup.getWidgets(); 

Add a widget that displays the nonvirtual block count metric. For custom widgets, the default visualization type is single value. If you want to use a different visualization technique, specify a different value for the VisualizationType property.

newWidget = sizeGroup.addWidget('Custom', 1);
newWidget.Title=('Nonvirtual Block Count'); 

Specify whether there are lines separating the custom widget from other widgets in the group. These commands specify that there is a line to the right of the widget. = false;
s.bottom = false;
s.left= false;
s.right= true;
newWidget.setSeparators([s, s, s, s]);

Save the configuration object. This command serializes the API information to an XML file.


Set the active configuration.


For a model, open the Metrics Dashboard.

metricsdashboard sf_car

Click the All Metrics button and run all metrics.

Introduced in R2018b