Main Content

Define Finite Source Objects

This example shows how to define a System object™ that performs a specific number of steps or specific number of reads from a file.

Use the FiniteSource Class and Specify End of the Source

  1. Subclass from finite source class.

     classdef RunTwice < matlab.System & ...
        matlab.system.mixin.FiniteSource
    
  2. Specify the end of the source with the isDoneImpl method. In this example, the source has two iterations.

     methods (Access = protected)
       function bDone = isDoneImpl(obj)
          bDone = obj.NumSteps==2
       end
    

Complete Class Definition File with Finite Source

classdef RunTwice < matlab.System & ...
    matlab.system.mixin.FiniteSource
  % RunTwice System object that runs exactly two times
  %  
  properties (Access = private)
    NumSteps
  end
  
  methods (Access = protected)
    function resetImpl(obj)
      obj.NumSteps = 0;
    end

    function y = stepImpl(obj)
      if ~obj.isDone()
        obj.NumSteps = obj.NumSteps + 1;
        y = obj.NumSteps;
      else
        y = 0;
      end
    end

    function bDone = isDoneImpl(obj)
      bDone = obj.NumSteps==2;
    end
  end
end

See Also

Related Topics