主要内容

设置

一次性为System对象设置任务

描述

例子

设置(obj执行特定于System对象™的一次性设置任务。

设置(obj、input1 inputN……)当这些设置任务需要样本输入来验证输入值时,执行一次性设置任务。

例子

初始化系统对象

这个例子展示了如何调用设置在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

其他工具箱中的示例

输入参数

全部折叠

在运行System对象之前要设置的系统对象。

选择功能

对于大多数System对象,您不需要调用设置.当你第一次调用System对象时,设置被称为。(见呼叫序列摘要.)你应该叫设置仅当您需要减少初始化的计算负载时。

介绍了R2010a