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
) and you intend to use the System
object in Simulink®, you must include the getNumInputsImpl
or getNumOutputsImpl
method in your
class definition file.
As with all System
object Impl
methods, you always set the getNumInputsImpl
and
getNumOutputsImpl
method's access to protected
because they are internal methods
that users do not directly call or run.
To update the algorithm to specify two inputs and two outputs, modify the stepImpl
method. 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
This example shows how to write a system object that allows changes to the number of inputs and outputs at setup time. To update the System object to accept either two or three inputs and outputs:
Modify stepImpl
to use varargin
and varargout
Add a nontunable property to control the number of inputs and outputs
Implement the associated getNumInputsImpl
and getNumOutputsImpl
methods to
specify two or three inputs and outputs.
% This property is nontunable and cannot be changed % after the after the object has been called properties (Nontunable) numInputsOutputs = 3; % Default value end 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
This example shows how to implement a System object whose number of inputs and outputs can change from call to call.
To update the algorithm to specify a variable number of inputs and outputs, modify the stepImpl
method to
include varargin
and varargout
. In this case, do not implement
getNumInputsImpl
or getNumOutputsImpl
because you do not want to restrict the number of
inputs or outputs.
classdef AddOne < matlab.System % ADDONE Compute output values one greater than the input values methods (Access = protected) function [varargout] = stepImpl(~,varargin) for i=1:nargin-1 varargout{i} = varargin{i}+1; end end end end
When you call the System object™, you can use any number of inputs.
obj = AddOne; a = obj(3)
a = 4
[a,b,c] = obj(3,6,9)
a = 4 b = 7 c = 10
[a,b] = obj(3,6)
a = 4 b = 7
If your System
object is used in Simulink, you cannot use a variable number of outputs. You must implement getNumInputsImpl
and
getNumOutputsImpl
to set the exact number of inputs and outputs.
getNumInputsImpl
| getNumOutputsImpl