Documentation

Specify Whether Output Is Fixed- or Variable-Size

This example shows how to specify that System object™ output is fixed- or variable-size.

This example shows how to specify that a System object output is fixed-size. Use the isOutputFixedSizeImpl method when Simulink® cannot infer the output type from the inputs during model compilation.

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

This example shows how to specify that a System object output is variable-size. Use the isOutputFixedSizeImpl method when Simulink cannot infer the output type from the inputs during model compilation.

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 isOutputFixedSizeImpl method to .

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

View the method in the complete class definition file.

classdef SimpleVarSize < matlab.System
    % untitled Add summary here
    %
    % This template includes the minimum set of functions required
    % to define a System object with discrete state.
    
    properties
        % Public, tunable properties.
    end
    
    properties (DiscreteState)
    end
    
    properties (Access = private)
        % Pre-computed constants.
    end
    
    methods (Access = protected)
        function y = stepImpl(obj,u)
            % Implement algorithm. Calculate y as a function of
            % input u and discrete states.
            y = u;
        end
    end
end


% % We start with a simple System object, SimpleVarSize Variable sized
% vectors in System objects simply means that the system object can support
% different sized vectors at its input for example

obj = SimpleVarSize

% obj = 
%  System: SimpleVarSize

%% First call to step gives the same output because the object only copies
% the input to the output. 
y = obj.step(1:5)
% y =
%      1     2     3     4     5

%% and then call with a vector of a different size
y = obj.step((1:3)')
% y =
%      1
%      2
%      3


%% By default System objects accept vectors of different sizes if the
% object's algorithm allows.  If a System object author chooses to disallow
% variable size vectors, he/she can implement the isInputSizeLockedImpl
% method.  isInputSizeLockedImpl accepts the port index and returns true 
% if the input size is locked (variable sized vectors are disallowed) and
% false if the input size is not locked (variable sized vectors are
% allowed).  
obj2 = SimpleLockedSize

%% Executing step the first time sets the input size for the System object
y = obj2.step(1:5)
%y =
%      1     2     3     4     5

%% Executing step again with a different size throws an error
y = obj2.step((1:5)')
% Error using SimpleLockedSize
% Changing the size on input 1 is not allowed without first calling
% the release() method. 

%% System objects can accept structures
% We can create structures as inputs to System objects
s.field1 = 1:5
s.field2 = (1:3)'
% s = 
%     field1: [1 2 3 4 5]
%     field2: [3x1 double]
  
%% step accepts structures as inputs
sout = obj2.step(s)
% sout = 
%     field1: [1 2 3 4 5]
%     field2: [3x1 double]

%% Since all structures are considered the same by System objects, they accept 
% changes in structure field sizes and new fields regardless of the
% return of isInputSizeLocked

s2.field1 = s.field2
s2.field3 = s.field1
% s2 = 
%     field1: [3x1 double]
%     field3: [1 2 3 4 5]s2.field3 = s.field1
sout2 = obj2.step(s2)
% sout2 = 
%     field1: [3x1 double]
%     field3: [1 2 3 4 5]

%% System objects can accept cell arrays
% We can create cell arrays as inputs to System objects
c = {'cars','trucks'}
% c = 
%    'cars'    'trucks'
  
%% step will not accept cells after accepting structures as they are a 
% different data type.
cout = obj2.step(c)
% Error using SimpleLockedSize
% Changing the size or datatype on input 1 is not allowed without
% first calling the release() method. 

% Calling release to unlock the System object allows it to accept input of
% a new data type.
obj2.release
cout = obj2.step(c)
% cout = 
%    'cars'    'trucks'

%% Since cell arrays have size just like other arrays, changes in size
% is detected by System objects.
c2 = c'
% c2 = 
%     'cars'
%     'trucks'
cout2 = obj2.step(c2)
% Error using SimpleLockedSize
% Changing the size on input 1 is not allowed without first calling
% the release() method.

% Note that obj2 is configured to lock its input size. 

%% Calling release on the System object allows accept different size cells
obj2.release
cout2 = obj2.step(c2)
% cout2 = 
%     'cars'
%     'trucks'

%% An object which does not lock its input size, such as SimpleVarSize, 
% can accept cell arrays with different sizes.

obj3 = SimpleVarSize
cout3 = obj3.step(c)
% cout3 = 
%     'cars'    'trucks'
cout4 = obj3.step(c2)
% cout4 = 
%     'cars'
%     'trucks'

See Also

|

More About

Was this topic helpful?