Main Content

setup

One-time set up tasks for System objects

Description

example

setup(obj)performs one-time setup tasks specific to the System object™.

setup(obj,input1,...,inputN)performs one-time setup tasks when those setup tasks require sample inputs to validate input values.

Examples

Initialize Counter System Object

This example shows how to callsetupon a System object™. In most cases, you do not need to callsetupdirectly becausesetupinitialization happens the first time you run your System object. Callsetupbefore running only if you have concerns about the execution time of initialization.

Create a System objectCounterwith a start value of 5. (See the full definition ofCounterin 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 theCounterobject,setupImplinitializes theStartValueproperty with the specified number at which to start counting. When you callsetup, the System object callssetupImpland also validates the input and property values. BecauseCounterhas defined these internal validation methods, you must givesetupan input value to validate.

Initialize theStartValuefor yourcountobject by callingsetupwith a placeholder input value. After initialization, run the object.

setup(count,0) count(2)
ans = 7

Full Definition of theCounterSystem 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

Input Arguments

collapse all

System object you want to set up before running the System object.

Alternative Functionality

For most System objects, you do not need to callsetup. When you call the System object for the first time,setupis called. (SeeSummary of Call Sequence.) You should callsetupseparately only if you need to reduce the computational load of initialization.

版本历史

Introduced in R2010a