setup
为 System object 一次性设置任务
说明
示例
初始化 Counter System object
此示例说明如何对 System object™ 调用 setup
。在大多数情况下,您不需要直接调用 setup
,因为 setup
初始化发生在您第一次运行 System object 时。仅当您担心初始化的执行时间时,才建议您在运行前调用 setup
。
创建一个起始值为 5 的 System object Counter
。(请参阅以下节中 Counter
的完整定义。)
count = Counter('StartValue',5)
count = Counter with properties: UseIncrement: true UseWrapValue: true StartValue: 5 Increment: 1 WrapValue: 10
在 Counter
对象的定义中,setupImpl
使用开始计数的指定数字初始化 StartValue
属性。当您调用 setup
时,System object 调用 setupImpl
并验证输入和属性值。由于 Counter
已定义这些内部验证方法,因此您必须为 setup
提供一个输入值进行验证。
通过使用一个占位符输入值调用 setup
,为您的 count
对象初始化 StartValue
。初始化后,运行该对象。
setup(count,0) count(2)
ans = 7
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
其他工具箱中的示例
Reduce Latency Due to Input Device Buffer (Audio Toolbox)
Byte Transmission Using UDP (DSP System Toolbox)
输入参数
替代功能
对于大多数 System object,您不需要调用 setup
。首次调用 System object 时,将调用 setup
。(请参阅调用序列摘要。)仅在需要减少初始化的计算负荷时,才应单独调用 setup
。
版本历史记录
在 R2010a 中推出