| MATLAB® | ![]() |
| On this page… |
|---|
Properties encapsulate the data that belongs to instances of classes. Data contained in properties can be public, protected, or private, it can be a fixed set of constant values, or it can be dependent on other values and calculated only when queried. You control these aspects of property behaviors by setting property attributes and by defining property-specific access methods that determine what happens when a property value is specified or queried.
See Property Attributes for a summary of property attributes.
In some ways, properties are like fields of a struct object. However, storing data in an object property provides more flexibility. Properties can:
Define a constant value that cannot be changed outside the class definition. See
Calculate its value based on the current value of other data. See Property Get Methods
Execute a function to determine if an attempt to assign a value meets a certain criteria. See Property Set Methods
Trigger an event notification when any attempt is made to get or set its value. See Property-Set and Query Events
Restrict access by other code to the property value. See the SetAccess and GetAccess attributes Property Attributes
Control whether its value is saved with the object in a MAT-file. See
There are two types of properties:
Stored properties — Use memory and are part of the object
Dependent properties — No allocated memory and the value is calculated by a get access method when queried
Can assign an initial value in the class definition
Property value is stored when you save the object to a MAT-file
Can use a set access method to control possible values, but no access methods are required
You want to be able to save the property value in a MAT-file
The property value is not dependent on other property values
Dependent properties save memory because property values that depend on other values can be calculated only when needed.
Define properties as dependent when you need to:
Compute the value of a property from other values (e.g., area is computed from Width and Height properties).
Provide a value in different formats depending on other values (e.g., size of a push button in values determined by the current setting of its Units property).
Provide a standard interface where a particular property might or might not be used depending on other values (e.g., different computer platforms might have different components on a toolbar).
Controlling Property Access provides information on defining property access methods.
![]() | Properties — Storing Class Data | Defining Properties | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |