setup
One-time set up tasks for System objects
Description
Examples
Initialize Counter System Object
This example shows how to call setup
on a System object™. In most cases, you do not need to call setup
directly because setup
initialization happens the first time you run your System object. Call setup
before running only if you have concerns about the execution time of initialization.
Create a System object Counter
with a start value of 5. (See the full definition of Counter
in the section below.)
count = Counter('StartValue',5)
count = Counter with properties: UseIncrement: true UseWrapValue: true StartValue: 5 Increment: 1 WrapValue: 10
In the definition of the Counter
object, setupImpl
initializes the StartValue
property with the specified number at which to start counting. When you call setup
, the System object calls setupImpl
and also validates the input and property values. Because Counter
has defined these internal validation methods, you must give setup
an input value to validate.
Initialize the StartValue
for your count
object by calling setup
with a placeholder input value. After initialization, run the object.
setup(count,0) count(2)
ans = 7
Full Definition of the Counter
System Object
type Counter.m
classdef Counter < matlab.System % COUNTER Compute an output value by incrementing the input value % All properties occur inside a properties declaration. % These properties have public access (the default) properties UseIncrement (1,1) logical = true % Use custom increment value. UseWrapValue (1,1) logical = true % Use max value. StartValue (1,1) {mustBeInteger,mustBePositive} = 1 % Value to start from. Increment (1,1) {mustBeInteger,mustBePositive} = 1 % What to add to Value every step. WrapValue (1,1) {mustBeInteger,mustBePositive} = 10 % Max value to wrap around. end properties(Access = protected) Value end methods % Constructor - Support name-value pair arguments when constructing object function obj = Counter(varargin) setProperties(obj,nargin,varargin{:}) end function set.Increment(obj,val) if val >= 10 error('The increment value must be less than 10'); end obj.Increment = val; end end methods (Access = protected) % Validate the object properties function validatePropertiesImpl(obj) if obj.UseIncrement && obj.UseWrapValue && ... (obj.WrapValue < obj.Increment) error('Wrap value must be greater than increment value'); end end % Validate the inputs to the object function validateInputsImpl(~,x) if ~isnumeric(x) error('Input must be numeric'); end end % Perform one-time calculations, such as computing constants function setupImpl(obj) obj.Value = obj.StartValue; end % Step function out = stepImpl(obj,in) if obj.UseIncrement % If using increment property, multiple the increment by the input. obj.Value = in*obj.Increment + obj.Value; else % If not using increment property, add the input. obj.Value = in + obj.Value; end if obj.UseWrapValue && obj.Value > obj.WrapValue % If UseWrapValue is true, wrap the value % if it is greater than the WrapValue. obj.Value = mod(obj.Value,obj.WrapValue); end out = obj.Value; end end end
Examples in Other Toolboxes
Reduce Latency Due to Input Device Buffer (Audio Toolbox)
Byte Transmission Using UDP (DSP System Toolbox)
Input Arguments
Alternative Functionality
For most System objects, you do not need to call setup
. When you call
the System object for the first time, setup
is called. (See Summary of Call Sequence.) You
should call setup
separately only if you need to reduce the computational
load of initialization.
Version History
Introduced in R2010a