Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Change Number of Inputs or Outputs

This example shows how to specify two inputs and two outputs to a System object™.

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.

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. For 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

To run this System object with two inputs and two outputs, use this syntax at the command line. Create the object, set the input values for two inputs and two outputs, and then, run the object. Finally, release the object so you can rerun it.

addOne = AddOne;
x1 = 3; 
x2 = 7; 

addOne.numInputsOutputs = 2;
[y1,y2] = addOne(x1,x2);

release(addOne); 

Use this syntax to use three inputs and outputs.

x1 = 3; 
x2 = 7; 
x3 = 14; 
addOne.numInputsOutputs = 3;
[y1,y2,y3] = 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 method has been called or when
   % the object is running.
   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 Topics

Was this topic helpful?