Limit Property Values to Finite List

When you want to create a System object™ property with a limited set of acceptable values, you can either use enumerations or property validation.

For a System object that is used in a MATLAB System block in Simulink® you can use enumerations or property validation. If you use enumerations, enumerations can also derive from Simulink.IntEnumType. You use this type of enumeration to add attributes (such as custom headers) to the input or output of the MATLAB System block. See Use Enumerated Data in Simulink Models (Simulink).

Property Validation with mustBeMember

To limit property values with property validation, you use the mustBeMember validation function.

This example defines a Style property that can have the values solid, dash, or dot. The default value is solid and the (1,1) defines the property as a scalar.

    properties
        Style (1,1) string {mustBeMember(Style, ["solid","dash","dot"])} = "solid";
    end

Enumeration Property

To use enumerated data in a System object, you refer to the enumerations as properties in your System object class definition and define your enumerated class in a separate class definition file.

To create an enumerated property, you need:

  • A System object property set to the enumeration class.

  • The associated enumeration class definition that defines all possible values for the property.

This example defines a color enumeration property for a System object. The definition of the enumeration class ColorValues is:

classdef ColorValues < int32
    enumeration
        blue (0)
        red (1)
        green (2)
    end
end
The ColorValues class inherits from int32 for code generation compatibility. Enumeration values must be valid MATLAB identifiers.

In the System object, the Color property is defined as a ColorValues object with blue as the default. The (1,1) defines the Color property as a scalar:

properties
   Color (1, 1) ColorValues = ColorValues.blue
end

Create a Whiteboard System object

This example shows the class definition of a Whiteboard System object, two types of finite list properties, and how to use the object. Each time you run the whiteboard object, it draws a line on a whiteboard.

Definition of the Whiteboard System object

type('Whiteboard.m')
classdef Whiteboard < matlab.System
    % Whiteboard Draw lines on a figure window
    %
    
    
    properties(Nontunable)
        Color (1, 1) ColorValues = ColorValues.blue
        Style (1,1) string {mustBeMember(Style, ["solid","dash","dot"])} = "solid";
    end

    methods (Access = protected)
        function stepImpl(obj)
            h = Whiteboard.getWhiteboard();
            switch obj.Style
                case "solid"
                    linestyle = "-";
                case "dash"
                    linestyle = "--";
                case "dot"
                    linestyle = ":";
            end
            plot(h, randn([2,1]), randn([2,1]), ...
                "Color",string(obj.Color), "LineStyle",linestyle);
        end
        
        function releaseImpl(~)
            cla(Whiteboard.getWhiteboard());
            hold on
        end
    end
    
    methods (Static)
        function a = getWhiteboard()
            h = findobj('tag','whiteboard');
            if isempty(h)
                h = figure('tag','whiteboard');
                hold on
            end
            a = gca;
        end
    end
end

Construct the System object.

greenInk = Whiteboard;
blueInk = Whiteboard;

Change the color and set the blue line style.

greenInk.Color = "green";
blueInk.Color = "blue";
blueInk.Style = "dot";

Draw a few lines.

for i=1:3
  greenInk();
  blueInk();
end

Clear the whiteboard

release(greenInk);
release(blueInk);

Related Topics