Documentation Center

  • Trial Software
  • Product Updates

Specify Discrete State Output Specification

This example shows how to specify the size, data type, and complexity of a discrete state property. You must use the getDiscreteStateSpecificationImpl method when your System object has a property that is defined with the DiscreteState attribute. This method indicates the output specifications when those specifications cannot be inferred during Simulink® model compilation.

Subclass from the Propagates Mixin Class

To use the getDiscreteStateSpecificationImpl method, you must subclass from both the matlab.System base class and from the Propagates mixin class.

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

Specify Discrete State Output Specification

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

Complete Class Definition File with Discrete State Output Specification

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 N = getNumInputsImpl(~)
         N = 2;
      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?