Main Content

Enumerations That Encapsulate Data

Enumeration Classes with Properties

Enumeration classes can define properties to store data values. The enumeration members represent specific values for these properties, which MATLAB® assigns in the class constructor. For information on defining enumeration classes, see Define Enumeration Classes.

Store Data in Properties

Note

Enumeration classes that subclass built-in numeric or logical classes cannot define or inherit properties. For more information on this kind of enumeration class, see Enumerations Derived from Built-In Classes.

Define properties in an enumeration class if you want to associate specific data with enumeration members, but do not need to inherit arithmetic, ordering, or other operations that MATLAB defines for specific built-in classes.

Representing Colors

Define an enumeration class to represent the RGB values of the colors in a color set. The Colors class defines names for the colors, each of which uses the RGB values as arguments to the class constructor:

classdef Colors
   properties
      R = 0
      G = 0
      B = 0
   end
   methods
      function c = Colors(r, g, b)
         c.R = r; c.G = g; c.B = b;
      end
   end
   enumeration
      Blueish   (18/255,104/255,179/255)
      Reddish   (237/255,36/255,38/255)
      Greenish  (155/255,190/255,61/255)
      Purplish  (123/255,45/255,116/255)
      Yellowish (1,199/255,0)
      LightBlue (77/255,190/255,238/255)
   end
end

You can access the property values via the enumeration member:

Colors.Reddish.R
ans =

    0.9294

Suppose that you want to create a plot with the new shade of red named Reddish:

a = Colors.Reddish;
[a.R,a.G,a.B]
ans =

    0.9294    0.1412    0.1490

Use these values by accessing the enumeration member properties. For example, the myPlot function accepts a Colors enumeration member as an input argument. The function accesses the RGB values defining the color from the property values.

function h = myPlot(x,y,LineColor)
   h = line('XData',x,'YData',y);
   r = LineColor.R;
   g = LineColor.G;
   b = LineColor.B;
   h.Color = [r g b];
end

Create a plot using a reddish color line:

h = myPlot(1:10,1:10,Colors.Reddish);

The Colors class encapsulates the definitions of a standard set of colors. You can change the enumeration class definition of the colors and not affect functions that use the enumerations.

Enumerations Defining Categories

The Cars class defines categories used to inventory automobiles. The Cars class derives from the CarPainter class, which derives from handle. The abstract CarPainter class defines a paint method, which modifies the Color property when a car is painted another color.

The Cars class uses the Colors enumeration members to specify a finite set of available colors. The exact definition of any given color can change independently of the Cars class.

classdef Cars < CarPainter
   enumeration
      Hybrid (2,'Manual',55,Colors.Reddish)
      Compact(4,'Manual',32,Colors.Greenish)
      MiniVan(6,'Automatic',24,Colors.Blueish)
      SUV    (8,'Automatic',12,Colors.Yellowish)
   end
   properties  (SetAccess = private)
      Cylinders
      Transmission
      MPG
      Color
   end
   methods
      function obj = Cars(cyl,trans,mpg,colr)
         obj.Cylinders = cyl;
         obj.Transmission = trans;
         obj.MPG = mpg;
         obj.Color = colr;
      end
      function paint(obj,colorobj)
         if isa(colorobj,'Colors')
            obj.Color = colorobj;
         else
            [~,cls] = enumeration('Colors');
            disp('Not an available color')
            disp(cls)
         end
      end
   end
end

The CarPainter class requires its subclasses to define a method called paint:

classdef CarPainter < handle
   methods (Abstract)
      paint(carobj,colorobj)
   end
end

Define an instance of the Cars class:

c1 = Cars.Compact;

The color of this car is Greenish, as defined by the Colors.Greenish enumeration:

c1.Color
ans = 

    Greenish 

Use the paint method to change the car color:

c1.paint(Colors.Reddish)
c1.Color
ans = 

    Reddish

Related Topics