Change Number of Step Inputs or Outputs

This example shows how to specify two inputs and two outputs for the step method.

If you specify the inputs and outputs to the stepImpl method, you do not need to specify the getNumInputsImpl and getNumOutputsImpl methods. If you have a variable number of inputs or outputs (using varargin or varargout), include the getNumInputsImpl or getNumOutputsImpl method, respectively, in your class definition file.

    Note:   You should only use getNumInputsImpl or getNumOutputsImpl methods to change the number of System object™ inputs or outputs. Do not use any other handle objects within a System object to change the number of inputs or outputs.

You always set the getNumInputsImpl and getNumOutputsImpl methods access to protected because they are internal methods that users do not directly call or run.

Update the Algorithm for Multiple Inputs and Outputs

Update the stepImpl method to specify two inputs and two outputs. You do not need to implement associated getNumInputsImpl or getNumOutputsImpl methods.

methods (Access = protected)
   function [y1,y2] = stepImpl(~,x1,x2)
      y1 = x1 + 1
      y2 = x2 + 1;
   end
end

Update the Algorithm and Associated Methods

Update the stepImpl method to use varargin and varargout. In this case, you must implement the associated getNumInputsImpl and getNumOutputsImpl methods to specify two or three inputs and outputs.

methods (Access = protected)
   function varargout = stepImpl(obj,varargin)
      varargout{1} = varargin{1}+1;
      varargout{2} = varargin{2}+1;
      if (obj.numInputsOutputs = 3)
         varargout{3} = varargin{3}+1;
      end
   end

   function validatePropertiesImpl(obj)
      if ~((obj.numInputsOutputs == 2) ||...
           (obj.numInputsOutputs == 3))
         error('Only 2 or 3 input and outputs allowed.');
      end
   end

   function numIn = getNumInputsImpl(obj)
      numIn = 3;
      if (obj.numInputsOutputs == 2)
         numIn = 2;
      end
   end

   function numOut = getNumOutputsImpl(obj)
      numOut = 3;
      if (obj.numInputsOutputs == 2)
         numOut = 2;
      end
   end
end

Use this syntax to run the algorithm with two inputs and two outputs.

x1 = 3;
x2 = 7;
[y1,y2] = step(AddOne,x1,x2);

To change the number of inputs or outputs, you must release the object before rerunning it.

release(AddOne)
x1 = 3;
x2 = 7;
x3 = 10
[y1,y2,y3] = step(AddOne,x1,x2,x3);

Complete Class Definition File with Multiple Inputs and Outputs

 classdef AddOne < matlab.System
% ADDONE Compute output values one greater than the input values
  
   % This property is nontunable and cannot be changed
   % after the setup or step method has been called.
   properties (Nontunable)
      numInputsOutputs = 3;   % Default value
     end

   % All methods occur inside a methods declaration.
   % The stepImpl method has protected access
   methods (Access = protected)
      function varargout = stepImpl(obj,varargin)
         if (obj.numInputsOutputs = 2)
            varargout{1} = varargin{1}+1;
            varargout{2} = varargin{2}+1;
         else
            varargout{1} = varargin{1}+1;
            varargout{2} = varargin{2}+1;
            varargout{3} = varargin{3}+1;
         end
      end

      function validatePropertiesImpl(obj)
         if ~((obj.numInputsOutputs == 2) ||
              (obj.numInputsOutputs == 3))
            error('Only 2 or 3 input and outputs allowed.');
         end
      end

      function numIn = getNumInputsImpl(obj)
         numIn = 3;
         if (obj.numInputsOutputs == 2)
            numIn = 2;
         end
      end

      function numOut = getNumOutputsImpl(obj)
         numOut = 3;
         if (obj.numInputsOutputs == 2)
            numOut = 2;
         end
      end
   end
end

See Also

|

Related Examples

More About

Was this topic helpful?