Defining Properties

Property Definition Block

The following illustration shows a typical property specification. The property and end keywords delineate a block of code that defines properties having the same attribute settings.

Assigning an Initial Value

The example above shows the Coefficients property specified as having an initial value that is an empty array of doubles.

You can initialize property values with MATLAB® expressions, but these expressions cannot refer to the class being defined in any way, except to call class static methods. MATLAB executes expressions that create initial property values only when initializing the class, which occurs just before the class is first used.

Accessing Property Values

Property access syntax is similar to MATLAB structure field syntax. For example, assume there is a polynomial class called polyno that defines a Coefficients property. If you created a polyno object p:

p = polyno([1 0 -2 -3]); % Create an instance p (this code does not execute)

you can access this property as follows:

c = p.Coefficients; % Assign the current property value to c
p.Coefficients = [4 0 -2 3 5]; % Assign new property values

When you access a property, the MATLAB runtime performs any operations that are required by the property, such as executing a property set or get access method and triggering property access events.

See Implementing a Set/Get Interface for Properties for information on how to define set and get methods for properties.

Inheritance of Properties

When you derive one class from another class, the derived (subclass) class inherits all the properties of the superclass. In general, subclasses should define only properties that are unique to that particular class. Superclasses should define properties that are used by more than one subclass.

Specifying Property Attributes

Attributes specified with the properties key word apply to all property definitions that follow in that block. If you want to apply attribute settings to certain properties only, reuse the properties keyword and create another property block for those properties.

For example, the following code shows the SetAccess attribute set to private for the IndependentVar and Order properties, but not for the Coefficients property:

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS