MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Limit Property Values to Finite List


When you want to create a System object property with a limited set of acceptable values,
you use enumerations. To use enumerated data in a System object in MATLAB or
Simulink, you refer to them in your System object class definition and define your
enumerated class in a separate class definition file.

For more information about enumerations, see “Enumerations”.

For a System object that is used in a MATLAB System block in Simulink, see “Use
Enumerated Data in Simulink Models” (Simulink).

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.


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).

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.

The System object property called Color and sets the default value to blue. The (1,1)
defines the size of the Color property:

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

34 System object Usage and Authoring

Free download pdf