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.
Note:
You should only use |
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 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 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.
addit = AddOne; x1 = 3; x2 = 7; [y1,y2] = addit(x1,x2);
To change the number of inputs or outputs, you must release the object before rerunning it.
release(addit) x1 = 3; x2 = 7; x3 = 10 [y1,y2,y3] = addit(x1,x2,x3);
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