Change the Number of Inputs
This example shows how to set the number of inputs for a System object™ with and without using getNumInputsImpl
.
If you have a variable number of inputs or outputs and you intend to use the System object in Simulink®, you must include the getNumInputsImpl
or getNumOutputsImpl
method in your class definition.
These examples show modifications for the number of inputs. If you want to change the number of outputs, the same principles apply.
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 are never called directly.
Allow up to Three Inputs
This example shows how to write a System object that allows the number of inputs to vary.
Update the stepImpl
method to accept up to three inputs by adding code to handle one, two, or three inputs. If you are only using this System object in MATLAB, getNumInputsImpl
and getNumOutputsImpl
are not required.
Full Class Definition
classdef AddTogether < matlab.System % Add inputs together methods (Access = protected) function y = stepImpl(~,x1,x2,x3) switch nargin case 2 y = x1; case 3 y = x1 + x2; case 4 y = x1 + x2 + x3; otherwise y = []; end end end end
Run this System object with one, two, and three inputs.
addObj = AddTogether; addObj(2)
ans = 2
addObj(2,3)
ans = 5
addObj(2,3,4)
ans = 9
Control the Number of Inputs and Outputs with a Property
This example shows how to write a System object that allows changes to the number of inputs and outputs before running the object. Use this method when your System object will be included in Simulink:
Add a nontunable property
NumInputs
to control the number of inputs.Implement the associated
getNumInputsImpl
method to specify the number of inputs.
Full Class Definition
classdef AddTogether2 < matlab.System % Add inputs together. The number of inputs is controlled by the % nontunable property |NumInputs|. properties (Nontunable) NumInputs = 3; % Default value end methods (Access = protected) function y = stepImpl(obj,x1,x2,x3) switch obj.NumInputs case 1 y = x1; case 2 y = x1 + x2; case 3 y = x1 + x2 + x3; otherwise y = []; end end function validatePropertiesImpl(obj) if ((obj.NumInputs < 1) ||... (obj.NumInputs > 3)) error("Only 1, 2, or 3 inputs allowed."); end end function numIn = getNumInputsImpl(obj) numIn = obj.NumInputs; end end end
Run this System object with one, two, and three inputs.
addObj = AddTogether2; addObj.NumInputs = 1; addObj(2)
ans = 2
release(addObj); addObj.NumInputs = 2; addObj(2,3)
ans = 5
release(addObj); addObj.NumInputs = 3; addObj(2,3,4)
ans = 9
See Also
getNumInputsImpl
| getNumOutputsImpl