Attribute Specification

Attribute Syntax

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:

properties (SetAccess = private)
   ScreenSize = getScreenSize
end

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

Note

Specify the value of a particular attribute only once in any component block.

Attribute Descriptions

For lists of supported attributes, see:

Attribute Values

When you specify attribute values, those values affect all the components defined within the defining 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:

classdef (AttributeName = attributeValue) ClassName
   ...
end

Simpler Syntax for true/false Attributes

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:

methods (~Static) 
   ...
end

Is the same as:

methods (Static = false)
   ...
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.

Related Topics