主要内容

Insert System Object Code UsingMATLABEditor

Define System Objects with Code Insertion

您可以从MATLAB定义系统对象®编辑器使用代码插入选项。选择这些选项时,MATLAB编辑器将在System Object™中添加预定义的属性,方法,状态,输入或输出。使用这些工具更快地创建和修改系统对象,并通过减少键入错误来提高精度。GUI在线Matlab略有区别,但功能是一样的。

要访问System对象编辑选项,请创建新的系统对象,或打开现有系统对象。

要将预定义的代码添加到系统对象,请从相应菜单中选择代码。例如,当您单击时Insert Property>Numeric, the MATLAB Editor adds the following code:

properties(Nontunable) Propertyend

The MATLAB Editor inserts the new property with the default nameProperty, which you can rename. If you have an existing properties group with theNontunableattribute, the MATLAB Editor inserts the new property into that group. If you do not have a property group, the MATLAB Editor creates one with the correct attribute.

Insert Options

Properties

Properties of the System object: Numeric, Logical, Enumeration, Positive Integer, Tunable Numeric, Private, Protected, and Custom. When you select Enumeration or Custom Properties, a separate dialog box opens to guide you in creating these properties.

方法

系统对象定义中常用的方法。MATLAB编辑器仅创建方法结构。您指定该方法的操作。

The插入方法menu organizes methods by categories, such asAlgorithm,Inputs and Outputs, 和Properties and States. When you select a method from the menu, the MATLAB Editor inserts the method template in your System object code. In this example, selecting插入方法>Release resourcesinserts the following code:

功能releaseImpl(obj)%释放资源,如文件句柄end

If a method from the插入方法menu is present in the System object code, that method is shown shaded on the插入方法menu:

States

属性包含DiscreteStateattribute.

Inputs / Outputs

Inputs, outputs, and related methods, such asValidate inputsDisallow input size changes.

选择输入或输出时,MATLAB编辑器会插入指定的代码stepImplmethod. In this example, selectingInsert>Input导致MATLAB编辑器插入所需的输入变量u2. The MATLAB Editor determines the variable name, but you can change it after it is inserted.

功能y = stepImpl(obj,u,u2)% Implement algorithm. Calculate y as a function of% input u and discrete states.y = u;end

Create a Temperature Enumeration

  1. Open a new or existing System object.

  2. 在Matlab编辑器中,选择Insert Property>Enumeration.

  3. 在里面Enumeration对话框,输入:

    1. Property NamewithTemperatureUnit.

    2. Enumeration Namewith温度高表值.

  4. Select theCreate new enumerationcheck box.

  5. 删除现有的枚举值-(minus) button.

  6. Add three an enumeration values with the+(+)对接on and the following values:

    • Fahrenheit

    • Celsius

    • Kelvin

  7. SelectFahrenheitas the default value by clickingDefault.

    现在的对话框显示如下所示:

  8. 要创建此枚举和关联的类,请单击Insert.

  9. 在MATLAB编辑器中,创建具有枚举定义的其他类文件。将枚举类定义文件保存为温度高表值.m.

    classdef温度温度值enumerationFahrenheit (0) Celsius (1) Kelvin (2)endend

    在里面System object class definition, the following code was added:

    properties(Nontunable) TemperatureUnit (1, 1) TemperatureUnitValues = TemperatureUnitValues.Fahrenheitend

For more information on enumerations, seeLimit Property Values to Finite List.

Create Custom Property for Freezing Point

  1. Open a new or existing System object.

  2. 在Matlab编辑器中,选择Insert Property>Custom Property.

  3. 在里面Custom Property dialog box, underSystem Object Attributes, selectNontunable. UnderMATLAB Property Attributes, select持续的. LeaveGetAccessas上市.SetAccessis grayed out because properties of type constant cannot be set using System object methods.

  4. ClickInsert和the following code is inserted into the System object definition:

    properties(Nontunable, Constant) Propertyend
  5. ReplaceProperty与您的财产。

    属性(非全额,常数)FreezingPointFahrenheit = 32;end

Add Method to Validate Inputs

  1. Open a new or existing System object.

  2. 在Matlab编辑器中,选择插入方法>Validate inputs.

    The MATLAB Editor inserts this code into the System object:

    功能validateInputsImpl(obj,u)% Validate inputs to the step method at initializationend

相关话题