Specify Output - MATLAB & Simulink (original) (raw)

Sometimes, Simulink® cannot infer the output characteristics of your System object™ during model compilation. To give Simulink more information about the System object output, use these methods.

Set Output Size

Specify the size of a System object output using the getOutputSizeImpl method. Use this method when Simulink cannot infer the output size from the inputs during model compilation. For instance, when the System object has multiple inputs or outputs or has variable-size output.

For variable-size inputs, the propagated input size frompropagatedInputSizeImpl differs depending on the environment.

Subclass from the matlab.System base class.

classdef CounterReset < matlab.System

Use the getOutputSizeImpl method to specify the output size.

methods (Access = protected) function sizeout = getOutputSizeImpl(~) sizeout = [1 1]; end end

View the method in the complete class definition file.

classdef CounterReset < matlab.System % 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 y = stepImpl(obj,u1,u2)
     % Add to count if u1 is above threshold
     % Reset if u2 is true
     if (u2)
       obj.Count = 0;
     elseif (any(u1 > obj.Threshold))
       obj.Count = obj.Count + 1;
     end
     y = obj.Count;
  end

  function resetImpl(obj)
     obj.Count = 0;
  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
  function flag = isInputSizeMutableImpl(~,idx)
     if idx == 1
       flag = true;
     else
       flag = false;
     end
  end

end end

Set Fixed- or Variable-Size Output

Specify the System object output is fixed-size. Fixed-size output is always the same size, while variable-size output can be different size vectors.

Simulink cannot infer the output size for variable-size output. To avoid errors, implement isOutputFixedSizeImpl and getOutputSizeImpl.

isOutputFixedSizeImpl accepts System object handle and returns an array of flags. Array size is equal to the size of the output ports. The value of the flags and their meanings are:

Subclass from the matlab.System base class.

classdef CounterReset < matlab.System

Use the isOutputFixedSizeImpl method to specify that the output is fixed size.

methods (Access = protected) function fixedout = isOutputFixedSizeImpl(~) fixedout = true; end end

View the method in the complete class definition file.

classdef CounterReset < matlab.System % 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 y = stepImpl(obj,u1,u2)
     % Add to count if u1 is above threshold
     % Reset if u2 is true
     if (u2)
       obj.Count = 0;
     elseif (any(u1 > obj.Threshold))
       obj.Count = obj.Count + 1;
     end
     y = obj.Count;
  end

  function resetImpl(obj)
     obj.Count = 0;
  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
  function flag = isInputSizeMutableImpl(~,idx)
     if idx == 1
       flag = true;
     else
       flag = false;
     end
  end

end end

Set Output Data Type

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 different input and output data types. If you want bus output, also use the getOutputDataTypeImpl method. To use bus output, you must define the bus data type in the base workspace and you must include the getOutputDataTypeImpl method in your class definition file.

For both examples, subclass from the matlab.System base class.

classdef DataTypeChange < matlab.System

Specify, in your class definition file, how to control the output data type from a MATLABSystem block. Use the getOutputDataTypeImpl method to change the output data type from double to single, or propagate the input as a double. It also shows how to cast the data type to change the output data type in the stepImpl method, if necessary.

methods (Access = protected) function out = getOutputDataTypeImpl(obj) if obj.Quantize == true out = 'single'; else out = propagatedInputDataType(obj,1); end end end

classdef DataTypeChange < matlab.System

properties(Nontunable) Quantize = false; end

methods(Access = protected) function y = stepImpl(obj,u) if obj.Quantize == true % Cast for output data type to differ from input. y = single(u); else % Propagate output data type. y = u; end end

  function out = getOutputDataTypeImpl(obj)
     if obj.Quantize == true
        out = 'single';
     else
        out = propagatedInputDataType(obj,1);
     end
  end

end end

This model shows propagated double data type.

This model shows the result of changing the data type from double to single. The Display block shows the effect of quantizing the data.

The block mask for the MATLAB System block includes an edit field to switch between using propagation (Quantize =false) and switching from double to single (Quantize = true).

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 % 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

Set Output Complexity

Specify whether a System object output is complex or real using theisOutputComplexImpl method. Use this method when Simulink cannot infer the output complexity from the inputs during model compilation.

Subclass from the matlab.System base class.

classdef CounterReset < matlab.System

Use the isOutputComplexImpl method to specify that the output is real.

methods (Access = protected) function cplxout = isOutputComplexImpl(~) cplxout = false; end end

View the method in the complete class definition file.

classdef CounterReset < matlab.System % 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 y = stepImpl(obj,u1,u2)
     % Add to count if u1 is above threshold
     % Reset if u2 is true
     if (u2)
       obj.Count = 0;
     elseif (any(u1 > obj.Threshold))
       obj.Count = obj.Count + 1;
     end
     y = obj.Count;
  end

  function resetImpl(obj)
     obj.Count = 0;
  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
  function flag = isInputSizeMutableImpl(~,idx)
     if idx == 1
       flag = true;
     else
       flag = false;
     end
  end

end end

Set Discrete State Output Specification

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 the matlab.System base class.

classdef CounterReset < matlab.System

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

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

View the method in the complete class definition file.

classdef CounterReset < matlab.System % 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 y = stepImpl(obj,u1,u2)
     % Add to count if u1 is above threshold
     % Reset if u2 is true
     if (u2)
       obj.Count = 0;
     elseif (any(u1 > obj.Threshold))
       obj.Count = obj.Count + 1;
     end
     y = obj.Count;
  end

  function resetImpl(obj)
     obj.Count = 0;
  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
  function flag = isInputSizeMutableImpl(~,idx)
     if idx == 1
       flag = true;
     else
       flag = false;
     end
  end

end end

See Also

Functions

Topics