You can define System objects from the MATLAB® Editor using code insertion options. When you select these options, the MATLAB Editor adds predefined properties, methods, states, inputs, or outputs to your System object™. Use these tools to create and modify System objects faster, and to increase accuracy by reducing typing errors.
To access the System object editing options, create a new System object, or open an existing one.
To add predefined code to your System object, select the code from the appropriate menu. For example, when you click Insert Property > Numeric, the MATLAB Editor adds the following code:
properties(Nontunable)
Property
end
The MATLAB Editor inserts the new property with the default
name Property
, which you can rename. If you have
an existing properties group with the Nontunable
attribute,
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, String Set, Positive Integer, Tunable Numeric, Private, Protected, and Custom. When you select String Set or Custom Properties, a separate dialog box opens to guide you in creating these properties. |
Methods | Methods commonly used in System object definitions. The MATLAB Editor creates only the method structure. You specify the actions of that method. The Insert Method menu organizes methods by categories, such as Algorithm, Inputs and Outputs, and 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 Insert Method > Release resources inserts the following code: function releaseImpl(obj) % Release resources, such as file handles end If an method from the Insert Method menu is present in the System object code, that method is shown shaded on the Insert Method menu:
|
States | Properties containing the |
Inputs / Outputs | Inputs, outputs, and related methods, such as Validate inputs and Lock input size. When
you select an input or output, the MATLAB Editor inserts the
specified code in the function y = stepImpl(obj,u,u2) % Implement algorithm. Calculate y as a function of % input u and discrete states. y = u; end |
Open a new or existing System object.
In the MATLAB Editor, select Insert Property > String Set.
In the String Set dialog box,
under Name, replace Color
with TemperatureUnit
.
Remove the existing Color
property
values with the - (minus) button.
Add a property value with the + (plus)
button. Enter Fahrenheit
.
Add another property value with +.
Enter Celsius
.
Add another property value with +.
Enter Kelvin
.
Select Fahrenheit
as the default
value by clicking Default.
The dialog box now looks as shown:
To create this string set and associated properties, with the default value selected, click Insert.
Examine the System object definition. The MATLAB Editor has added the following code:
properties (Nontunable) TemperatureUnit = 'Fahrenheit'; end properties(Constant, Hidden) TemperatureUnitSet = matlab.system.StringSet({'Fahrenheit','Celsius','Kelvin'}); end
For more information on the StringSet
class,
see matlab.System.StringSet
.
Open a new or existing System object.
In the MATLAB Editor, select Insert Property > Custom Property.
In the Custom Property dialog box, under System
Object Attributes, select Nontunable.
Under MATLAB Property Attributes, select Constant.
Leave GetAccess as public
. SetAccess is
grayed out because properties of type constant can not be set using System object methods.
The dialog box now looks as shown:
To insert the property into the System object code, click Insert.
properties(Nontunable, Constant) Property end
Replace Property
with your property.
properties(Nontunable, Constant) FreezingPointFahrenheit = 32; end
Open a new or existing System object.
In the MATLAB Editor, select Insert Method > Lock input size.
The MATLAB Editor inserts this code into the System object:
function flag = isInputSizeLockedImpl(obj,index) % Return true if input size is not allowed to change while % system is running flag = true; end