Main Content

Using a Class to Display Graphics

Class Calculates Area

The CircleArea class shows the syntax of a typical class definition. This class stores a value for the radius of a circle and calculates the area of the circle when you request this information. CircleArea also implements methods to graph, display, and create objects of the class.

To use the CircleArea class, copy this code into a file named CircleArea.m and save this file in a folder that is on the MATLAB® path.

classdef CircleArea
   properties
      Radius
   end
   properties (Constant)
      P = pi
   end
   properties (Dependent)
      Area
   end
   methods
      function obj = CircleArea(r)
         if nargin > 0
            obj.Radius = r;
         end
      end
      function val = get.Area(obj)
         val = obj.P*obj.Radius^2;
      end
      function obj = set.Radius(obj,val)
         if val < 0
            error('Radius must be positive')
         end
         obj.Radius = val;
      end
      function plot(obj)
         r = obj.Radius;
         d = r*2;
         pos = [0 0 d d];
         curv = [1 1];
         rectangle('Position',pos,'Curvature',curv,...
            'FaceColor',[.9 .9 .9])
         line([0,r],[r,r])
         text(r/2,r+.5,['r = ',num2str(r)])
         title(['Area = ',num2str(obj.Area)])
         axis equal
      end
      function disp(obj)
         rad = obj.Radius;
         disp(['Circle with radius: ',num2str(rad)])
      end
   end
   methods (Static)
      function obj = createObj
         prompt = {'Enter the Radius'};
         dlgTitle = 'Radius';
         rad = inputdlg(prompt,dlgTitle);
         r = str2double(rad{:});
         obj = CircleArea(r);
      end
   end
end

Use the CircleArea Class

Create an object using the dialog box:

ca = CircleArea.createObj

Add a value for radius and click OK.

Dialog for entering radius

Query the area of the defined circle:

ca.Area
ans =

  164.2202

Call the overloaded plot method:

plot(ca)

Graph of circle with radius 7.23

Description of Class Definition

Class definition code begins with the classdef keyword followed by the class name:

classdef CircleArea

Define the Radius property within the properties-end keywords. Use default attributes:

   properties
      Radius
   end

Define the P property as Constant (Define Class Properties with Constant Values). Call the pi function only once when class is initialized.

   properties (Constant)
      P = pi
   end

Define the Area property as Dependent because its value depends on the Radius property.

   properties (Dependent)
      Area
   end

The CircleArea class constructor method has the same name as the class and accepts the value of the circle radius as an argument. This method also allows no input arguments. (Class Constructor Methods)

   methods
      function obj = CircleArea(r)
         if nargin > 0
            obj.Radius = r;
         else
            obj.Radius = 0;
         end
      end

Because the Area property is Dependent, the class does not store its value. The get.Area method calculates the value of the Area property whenever it is queried. (Get and Set Methods for Dependent Properties)

      function val = get.Area(obj)
         val = obj.P*obj.Radius^2;
      end

The set.Radius method tests the value assigned to the Radius property to ensure that the value is not less than zero. MATLAB calls set.Radius to assign a value to Radius. (Property Get and Set Methods)

      function obj = set.Radius(obj,val)
         if val < 0
            error('Radius must be positive')
         end
         obj.Radius = val;
      end

The CircleArea class overloads the plot function. The plot method uses the rectangle function to create a circle and draws the radius. (Overload Functions in Class Definitions)

      function plot(obj)
         r = obj.Radius;
         d = r*2;
         pos = [0 0 d d];
         curv = [1 1];
         rectangle('Position',pos,'Curvature',curv)
         line([0,r],[r,r])
         text(r/2,r+.5,['r = ',num2str(r)])
         axis equal
      end

The CircleArea class overloads the disp function to change the way MATLAB displays objects in the command window.

      function disp(obj)
         rad = obj.Radius;
         disp(['Circle with radius: ',num2str(rad)])
      end
end
methods (Static)

The CircleArea class defines a Static method that uses a dialog box to create an object. (Static Methods)

      function obj = createObj
         prompt = {'Enter the Radius'};
         dlgTitle = 'Radius';
         rad = inputdlg(prompt,dlgTitle);
         r = str2double(rad{:});
         obj = CircleArea(r);
      end

End of Static methods block and end of classdef block.

   end
end