Properties
- For properties that do not change, define them in as Nontunable properties.
Tunable properties have slower access times than Nontunable properties - Whenever possible, use the protected or private attribute instead of the public
attribute for a property. Some public properties have slower access times than
protected and private properties. - If properties are accessed more than once in the stepImpl method, cache those
properties as local variables inside the method. A typical example of multiple property
access is a loop. Iterative calculations using cached local variables run faster than
calculations that must access the properties of an object. When the calculations for the
method complete, you can save the local cached results back to the properties of that
System object. Copy frequently used tunable properties into private properties. This
best practice also applies to the updateImpl and outputImpl methods.
For example, in this code k is accessed multiple times in each loop iteration, but is
saved to the object property only once.
function y = stepImpl(obj,x)
k = obj.MyProp;
for p=1:100
y = k * x;
k = k + 0.1;
end
obj.MyProp = k;
end
- Default values of properties are shared across all instances of an object. Two instances
of a class can access the same default value if that property has not been overwritten
by either instance.
Text Comparisons
Do not use character vector comparisons or character vector-based switch statements in
the stepImpl method. Instead, create a method handle in setupImpl. This handle
points to a method in the same class definition file. Use that handle in a loop in
stepImpl.
This example shows how to use method handles and cached local variables in a loop to
implement an efficient object. In setupImpl, choose myMethod1 or myMethod2 based on
a character vector comparison and assign the method handle to the pMethodHandle
Tips for Defining System Objects