Specify Discrete State Output Specification

This example shows how to specify the size, data type, and complexity of a discrete state property using the getDiscreteStateSpecificationImpl method. Use this method when your System object™ has a property with the DiscreteState attribute and Simulink® cannot infer the output specifications during model compilation.

Subclass from both the matlab.System base class and from the Propagates mixin class.

 classdef CounterReset < matlab.System & ...
     matlab.system.mixin.Propagates

Use the getDiscreteStateSpecificationImpl method to specify the size and data type. Also specify the complexity of a discrete state property, which is used in the counter reset example.

methods (Access = protected)
   function [sz,dt,cp] = getDiscreteStateSpecificationImpl(~,name)
      sz = [1 1];
      dt = 'double';
      cp = false;
   end
end

View the method in the complete class definition file.

classdef CounterReset < matlab.System & matlab.system.mixin.Propagates
   % CounterReset Count values above a threshold
    
   properties
      Threshold = 1
   end
  
   properties (DiscreteState)
      Count
   end
  
   methods (Access = protected)
      function setupImpl(obj)
         obj.Count = 0;
      end
    
      function resetImpl(obj)
         obj.Count = 0;
      end
    
      function y = stepImpl(obj,u1,u2)
         % Add to count if u1 is above threshold
         % Reset if u2 is true
         if (u2)
            obj.Count = 0;
         elseif (u1 > obj.Threshold)
            obj.Count = obj.Count + 1;
         end
         y = obj.Count;
      end
    
      function [sz,dt,cp] = getDiscreteStateSpecificationImpl(~,name)
         sz = [1 1];
         dt = 'double';
         cp = false;
      end
      function dataout = getOutputDataTypeImpl(~)
         dataout = 'double';
      end
      function sizeout = getOutputSizeImpl(~)
         sizeout = [1 1];
      end
      function cplxout = isOutputComplexImpl(~)
         cplxout = false;
      end
      function fixedout = isOutputFixedSizeImpl(~)
         fixedout = true;
      end
   end
end

See Also

|

More About

Was this topic helpful?