setup
One-time set up tasks for System objects
Description
Examples
Initialize Counter System Object
This example shows how to callsetup
on a System object™. In most cases, you do not need to callsetup
directly becausesetup
initialization happens the first time you run your System object. Callsetup
before running only if you have concerns about the execution time of initialization.
Create a System objectCounter
with a start value of 5. (See the full definition ofCounter
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 theCounter
object,setupImpl
initializes theStartValue
property with the specified number at which to start counting. When you callsetup
, the System object callssetupImpl
and also validates the input and property values. BecauseCounter
has defined these internal validation methods, you must givesetup
an input value to validate.
Initialize theStartValue
for yourcount
object by callingsetup
with a placeholder input value. After initialization, run the object.
setup(count,0) count(2)
ans = 7
Full Definition of theCounter
System Object
typeCounter.m
classdef计数器< matlab。系统%计数器计算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 callsetup
. When you call the System object for the first time,setup
is called. (SeeSummary of Call Sequence.) You should callsetup
separately only if you need to reduce the computational load of initialization.
版本历史
Introduced in R2010a