Representing Hardware with Classes

Objective

This example implements a class to represent an optical multiplex card. These cards typically have several input ports and an output port. The MuxCard class represents the ports by the port names and port data rates. The output rate of a multiplex card is the sum of the input port data rates.

Why Derive from int32

The MuxCard class derives from the int32 class because 32–bit integers represent the input port data rates. The MuxCard class inherits the methods of the int32 class, which simplifies the implementation of this subclass. For example, numeric array indexing and arithmetic operations work on MuxCard objects because the class inherits these operations from the int32 class.

Implementation

Here is the definition of the MuxCard class. Notice that the input port rates initialize the int32 portion of class.

classdef MuxCard < int32
   properties
      InPutNames 
      OutPutName 
   end
   
   properties (Dependent = true)
      OutPutRate
   end
   
   methods
      function obj = MuxCard(inptnames, inptrates, outpname)
         obj = obj@int32(inptrates); 
         obj.InPutNames = inptnames;
         obj.OutPutName = outpname;
      end
      
      function x = get.OutPutRate(obj)
         x = sum(obj); 
      end
      
      function x = subsref(card, s)
         if strcmp(s(1).type,'.')
            base = subsref@int32(card, s(1));
            if isscalar(s)
               x = base;
            else
               x = subsref(base, s(2:end));
            end
         else
            x = subsref(int32(card), s);
         end
      end
   end
end

Construct MuxCard Object

The constructor takes three arguments:

  • inptnames — Cell array of input port names

  • inptrates — Vector of input port rates

  • outpname — Name for the output port

omx = MuxCard({'inp1','inp2','inp3','inp4'},[3 12 12 48],'outp')
omx = 

  1x4 MuxCard array with properties:

    InPutNames: {'inp1'  'inp2'  'inp3'  'inp4'}
    OutPutName: 'outp'
    OutPutRate: 75

  int32 data:
           3          12          12          48

Call Methods of int32

Use a MuxCard object like an array of int32 values. For example, this indexing statement accesses the data in the object to determine the names of the input ports that have a rate of 12:

omx.InPutNames(omx==12)
ans = 
    'inp2'    'inp3'

The indexing statement generates a logical array index:

omx == 12
ans =

     0     1     1     0

Indexing the MuxCard object accesses the int32 vector of input port rates:

omx(1:2)
ans =
           3          12

The OutPutRate property get access method uses sum to sum the output port rates:

omx.OutPutRate
ans =
    75

Related Topics