Main Content

Property Attributes

Purpose of Property Attributes

You can specify attributes in the class definition to customize the behavior of properties for specific purposes. Control characteristics like access, data storage, and visibility of properties by setting attributes. Subclasses do not inherit superclass member attributes.

Specify Property Attributes

Assign property attributes on the same line as the properties keyword.

properties (Attribute1 = value1, Attribute2 = value2,...)
   ...
end

For example, define a property Data with private access.

properties (Access = private)
   Data
end

You can use a simpler syntax for attributes whose values are true. The attribute name by itself implies true, and adding the not operator (~) to the name implies false. For example, this block defines abstract properties.

properties (Abstract)
   ...
end

Table of Property Attributes

All properties support the attributes listed in this table. Attribute values apply to all properties defined within the properties...end code block that specifies the nondefault values. Attributes that you do not explicitly defined take their default values.

Property Attributes

Attribute

Values

Additional Information

AbortSet

  • true – MATLAB® does not set the property value or call a set method if the new value is the same as the current value.

  • false (default) – MATLAB sets the property value regardless of the current value.

For handle classes only. Setting AbortSet to true also prevents the triggering of property PreSet and PostSet events.

For more information, see Assignment When Property Value Is Unchanged.

Abstract

  • true – The property has no implementation, but a concrete subclass must override this property without Abstract set to true.

  • false (default) – The property is concrete and does not need to be overridden in a subclass.

Abstract properties cannot define set or get access methods. See Property Get and Set Methods.

Abstract properties cannot define initial values.

A sealed class cannot define abstract members.

Access

  • public (default) – The property can be accessed from any code.

  • protected – The property can be accessed from the defining class or its subclasses.

  • private – The property can be accessed only by members of the defining class.

  • List of classes that have get and set access to this property. Specify classes as a single matlab.metadata.Class object or a cell array of matlab.metadata.Class objects. See Property Access Lists for more information.

Use Access to set SetAccess and GetAccess to the same value.

Specifying Access as an empty cell array, {}, is the same as private access.

See Class Members Access for more information.

Constant

  • true – The property has the same value in all instances of the class.

  • false (default) – The property value can vary between instances.

Subclasses inherit constant properties but cannot change them.

Constant properties cannot also be defined as dependent.

The value of SetAccess is ignored for constant properties.

See Define Class Properties with Constant Values for more information.

Dependent

  • true – The property value is not stored in the object. The value is calculated when the property is accessed.

  • false (default) – The property value is stored in the object.

You can define set methods for dependent properties, but the set method cannot actually set the value of the property. It can take other actions, such as setting the value of another property. See When to Use Set Methods with Dependent Properties for an example.

Values returned by dependent property get methods are not considered when testing for object equality using isequal.

GetAccess

  • public (default) – The property can be read from any code.

  • protected – The property can be read from the defining class or its subclasses.

  • private – The property can be read only by members of the defining class.

  • List of classes that can read this property. Specify classes as a single matlab.metadata.Class object or a cell array of matlab.metadata.Class objects. See Property Access Lists for more information.

Specifying GetAccess as an empty cell array, {}, is the same as private access.

In the Command Window, MATLAB does not display the names and values of properties with protected or private GetAccess.

All subclasses must specify the same values as the superclass for the property SetAccess and GetAccess attributes.

See Class Members Access for more information.

GetObservable

  • true – You can create listeners for handle class properties. The listeners are called whenever property values are queried.

  • false (default) – Listeners do not have access to this property.

See Property-Set and Query Events for more information.

Hidden

  • true – The property is not visible in property lists or in results from calls to get, set, or the properties functions.

  • false (default) – The property is visible.

In the Command Window, MATLAB does not display the names and values of properties whose Hidden attribute is true. However, hidden properties are visible in the Class Diagram Viewer app.

NonCopyable

  • true – The property value is not copied when the object that defines it is copied.

  • false (default) – The property value is copied when the object is copied.

You can set NonCopyable to true only in handle classes.

For more information, see Exclude Properties from Copy.

PartialMatchPriority

Positive integer – Defines the relative priority of partial property name matches used in get and set methods. The default value is 1.

Use only with subclasses of matlab.mixin.SetGet.

For more information, see Set Priority for Matching Partial Property Names.

SetAccess

  • public (default) – The property can be set from any code.

  • protected – The property can be set from the defining class or its subclasses.

  • private – The property can be set only by members of the defining class.

  • immutable – The property can be set only in the constructor.

  • List of classes that have set access to this property. Specify classes as a single matlab.metadata.Class object or a cell array of matlab.metadata.Class objects. See Property Access Lists for more information.

All subclasses must specify the same values as the superclass for the property SetAccess and GetAccess attributes.

For more information, see Class Members Access, Properties Containing Objects, and Mutable and Immutable Properties.

SetObservable

  • true – You can create listeners for handle class properties. The listeners are called whenever property values are set.

  • false (default) – Listeners do not have access to this property.

See Property-Set and Query Events for more information.

Transient

  • true – The property value is not saved when the object is saved to a file or sent from MATLAB to another program, such as a MATLAB Engine application.

  • false (default) – The property value is saved when the object is saved.

See Default Save and Load Process for Objects for more information.

WeakHandle (since R2024b)

  • true – The property holds a weak reference to a handle object. Weak references to an object do not prevent that object from being deleted.

  • false (default) – The property holds a value class object or a strong reference to a handle object.

Properties defined as WeakHandle can only take handle object arrays as values.

All properties defined with the WeakHandle attribute must use class validation. Default values are supported, but they must be empty or an array of invalid handles.

WeakHandle cannot be used for properties containing classes that inherit from matlab.indexing.RedefinesParen or that customize indexing by overriding subsref or subsasgn.

See Weak Reference Handles for examples and more information.

Framework attributes

Classes that use certain framework base classes have framework-specific attributes. See the documentation for the specific base class you are using for information on these attributes.

Property Access Lists

You can use lists of matlab.metadata.Class instances for the Access, GetAccess, and SetAccess attributes. For example, this class declares access lists for the Prop1 and Prop2 properties.

classdef PropertyAccess
   properties (GetAccess = {?ClassA, ?ClassB})
      Prop1
   end
   properties (Access = ?ClassC)
      Prop2
   end
end

For Prop1:

  • Classes ClassA and ClassB have get access to Prop1.

  • All subclasses of ClassA and ClassB have get access to Prop1.

  • Access lists are not inherited, so subclasses of PropertyAccess do not have get access to Prop1 unless they explicitly define that access.

For Prop2:

  • ClassC has get and set access to Prop2.

  • All subclasses of ClassC have get and set access to Prop2.

  • Access lists are not inherited, so subclasses of PropertyAccess do not have access to Prop2 unless they explicitly define that access.

Related Topics