Syntax Reference & Specifying Attributes

Attribute Syntax

For a quick reference to all attributes, see Attribute Tables.

Attributes modify the behavior of classes and class components (properties, methods, and events). Attributes enable you to define useful behaviors without writing complicated code. For example, you can create a read-only property by setting its SetAccess attribute to private, but leaving its GetAccess attribute set to public (the default):

properties (SetAccess = private)
   ScreenSize = getScreenSize;
end

All class definition blocks (classdef, properties, methods, and events) support specific attributes and all attributes have default values. Specify attribute values only in cases where you want to change from the default value to another predefined value.

Attribute Descriptions

For lists of supported attributes, see:

  • Class Attributes
  • Property Attributes
  • Method Attributes
  • Event Attributes
  • Specifying Attribute Values

When you specify attribute values, these values affect all the components defined within the definition block. For example, the following property definition blocks set the:

  • AccountBalance property SetObservable attribute to true
  • SSNumber and CreditCardNumber properties’ Hidden attribute to true and SetAccess attribute to private.

Defining properties with different attribute settings requires multiple properties blocks.

properties (SetObservable = true) 
   AccountBalance
end
properties (SetAccess = private, Hidden = true)
   SSNumber
   CreditCardNumber
end

Specified multiple attributes in a comma-separated list, as shown in the previous example.

When specifying class attributes, place the attribute list directly after the classdef keyword:

Simpler Syntax for true/false Attributes

classdef (Sealed = true) myclass
   ...
end

You can use a simpler syntax for attributes whose values are true or false — the attribute name alone implies true and adding the not operator (~) to the name implies false. For example:

methods (Static)
   ...
end

is the same as:

methods (Static = true)
   ...
end

Use the not operator before an attribute name to define it as false:

is the same as:

methods (~Static) 
   ...
end

All attributes that take a logical value (that is, true or false) have a default value of false. Therefore, specify an attribute only if you want to set it to true.

methods (Static = false)
   ...
end

You may also like...