一次性为System对象设置任务
这个例子展示了如何调用设置
在System对象上。在大多数情况下,你不需要打电话设置
直接原因设置
初始化发生在您第一次运行System对象时。调用设置
只有当您担心初始化的执行时间时才运行。
创建System对象计数器
初始值为5。(见完整定义计数器
在下面的章节中。)
数=计数器(“StartValue”5)
count =计数器的属性:UseIncrement: 1 UseWrapValue: 1 StartValue: 5 Increment: 1 WrapValue: 10
在定义中计数器
对象,setupImpl
初始化StartValue
属性指定开始计数的数字。当你打电话设置
,系统对象调用setupImpl
并验证输入值和属性值。因为计数器
已经定义了这些内部验证方法,您必须给出设置
要验证的输入值。
初始化StartValue
为你的数
对象通过调用设置
使用占位符输入值。初始化后,运行对象。
设置(计数,0)数(2)
ans = 7
全面定义计数器
系统对象
类型Counter.m
< matlab. classdef CounterSystem % COUNTER通过增加输入值%计算输出值。所有属性都出现在属性声明中。UseIncrement (1,1) logical = true %使用自定义增量值。UseWrapValue (1,1) logical = true %使用最大值。StartValue (1,1) {mustBeInteger,mustBePositive} = 1%起始值。Increment (1,1) {mustBeInteger,mustBePositive} = 1%每一步要添加的值。WrapValue (1,1) {mustBeInteger,mustBePositive} = 10%最大环绕值。% Constructor -在构造对象函数时支持名称-值对参数obj = Counter(varargin) setProperties金宝app(obj,nargin,varargin{:}) end function set.Increment(obj,val) if val >= 10 error('The increment Value must be less than 10');obj。增量= 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
减少由于输入设备缓冲区造成的延迟(音频工具箱)
使用UDP进行字节传输(DSP系统工具箱)
对于大多数System对象,您不需要调用设置
.当你第一次调用System对象时,设置
被称为。(见呼叫序列摘要.)你应该叫设置
仅当您需要减少初始化的计算负载时。