Documentation

Set Output Data Type

This example shows how to specify the data type of a System object™ output using the getOutputDataTypeImpl method. A second example shows how to specify a gain object with bus output. Use this method when Simulink® cannot infer the data type from the inputs during model compilation or when you want bus output. To use bus output, you must define the bus data type in the base work space and you must include the getOutputDataTypeImpl method in your class definition file.

For both examples, subclass from both the matlab.System base class and the matlab.system.mixin.Propagates mixin class.

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

Use the getOutputDataTypeImpl method to specify the output data type as a double.

methods (Access = protected)
   function dataout = getOutputDataTypeImpl(~)
      dataout = 'double';
   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)
         if strcmp(name,'Count')
            sz = [1 1];
            dt = 'double';
            cp = false;
         else
            error(['Error: Incorrect State Name: ', name.']);
         end
      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

Use the getOutputDataTypeImpl method to specify the output data type as a bus. Specify the bus name in a property.

properties(Nontunable)
   OutputBusName = 'bus_name'; 
end

methods (Access = protected)
   function out = getOutputDataTypeImpl(obj)
      out = obj.OutputBusName;
   end
end

View the method in the complete class definition file. This class definition file also includes code to implement a custom icon for this object in the MATLAB System block

classdef busGain < matlab.System & matlab.system.mixin.Propagates
% busGain Apply a gain of two to bus input.

   properties
      GainK = 2;
   end
  
   properties(Nontunable)
      OutputBusName = 'bus_name'; 
   end

   methods (Access=protected)
      function out = stepImpl(obj,in)
         out.a = obj.GainK * in.a;
         out.b = obj.GainK * in.b;
      end

      function out = getOutputSizeImpl(obj)
         out = propagatedInputSize(obj, 1);
      end
    
      function out = isOutputComplexImpl(obj)
         out = propagatedInputComplexity(obj, 1);
      end
    
      function out = getOutputDataTypeImpl(obj)
         out = obj.OutputBusName;
      end
    
      function out = isOutputFixedSizeImpl(obj)
         out = propagatedInputFixedSize(obj,1);
      end
   end
end

See Also

|

More About

Was this topic helpful?