The Wayback Machine - https://web.archive.org/web/20110526065951/http://www.mathworks.com:80/help/techdoc/matlab_oop/bsmx1no-1.html
Skip to Main Content Skip to Search
Product Documentation

Enumerations That Encapsulate Data

Basic Knowledge

The material presented in this section builds on an understanding of the information provided in the following sections.

Store Data in Properties

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

Suppose you want to use a particular set of colors in all your graphs. You can define an enumeration class to represent the RGB values of the colors in your 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

Suppose you want to specify the new shade of red named Reddish:

a = Colors.Reddish;
a.R

ans =

    0.9294

a.G

ans =

    0.1412

a.B

ans =

    0.1490

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

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

Create a plot using a reddish color line:

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

The Colors class encapsulates the definitions of a standard set of colors. These definitions can change in the Colors class without affecting functions that use the Colors enumerations.

Enumerations Defining Categories

Suppose 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 if a car is painted another color.

The Cars class uses Colors enumerations 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

Suppose you 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
  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A; sessions led by MATLAB experts.

 © 1984-2011- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS