Hide Enumeration Members
Hiding enumeration members enables class authors to change enumeration member names without causing incompatibilities in existing code. To hide members, create an enumeration block that sets the Hidden
attribute. Members defined in a Hidden
enumeration block are not visible when enumeration members are queried using the enumeration
function.
When an enumeration class derives from another class, such as a numeric or logical class, then each member can have a value associated with it. If two members have the same value assigned to them, then the member defined first in the class definition masks the second member. Both names are valid enumeration members, but the first one defined is the primary member. While masking makes it possible to use one member name in place of another, it does not hide the secondary name from the class users.
Using the Hidden
attribute removes the masked member names from user view. For example, the HighlightColor
class defines enumeration members that represent syntax highlighting colors.
classdef HighlightColor < int32 enumeration red (1) green (2) blue (3) end end
A new version of the class uses more descriptive member names, but the class needs to avoid breaking existing code that uses the original member names, red, green, and blue. Using the Hidden
attribute for enumeration members enables the class to hide the original members.
classdef HighlightColor < int32 enumeration error (1) comment (2) keyword (3) end enumeration (Hidden) red (1) green (2) blue (3) end end
Code that uses the original member names continues to work. For example, existing references to the now-hidden member HighlightColor.blue
is compatible with the same-valued nonhidden member HighlightColor.keyword
.
a = HighlightColor.blue
a = HighlightColor enumeration keyword
a == HighlightColor.Keyword
ans = logical 1
For enumeration members that represent values, the first member defined in the class is the primary member for that value. For example, in the HighlightColor
class, keyword
is the primary member and blue
is the secondary member, both representing the value 3. Typically, the primary member is not hidden while the secondary member is hidden. However, if the class design requires that the primary member is hidden, then the secondary member must be hidden too.
Hide Pure Enumerations
Pure enumeration members have no underlying values, so there is no way to identify one member as a replacement for another. However, you can use the Hidden
attribute to remove a member from the user view while avoiding incompatibilities with existing uses of the hidden member.
For example, the PCComponents
class defines enumerations that are used in an online form for a computer order. While the FloppyDrive
component is obsolete, the enumeration member can remain in the class as a hidden member. The form can exclude FloppyDrive
from the list of choices, but the class author can keep this member available so that existing forms that refer to FloppyDrive
are still valid.
classdef PCComponents enumeration USBSlots CDPlayer end enumeration (Hidden) FloppyDrive end end
Find Hidden Enumeration Members
Find information about hidden enumeration members using class metadata. The matlab.metadata.EnumerationMember
class provides information on enumeration
members. For example, accessing the metadata for the HighlightColor
class
used in preceding examples can show the names of hidden members.
mc =?HighlightColor
mc = Class with properties: Name: 'HighlightColor' Description: '' DetailedDescription: '' Hidden: 0 Sealed: 0 Abstract: 0 Enumeration: 1 ConstructOnLoad: 0 HandleCompatible: 0 InferiorClasses: [1×1 matlab.metadata.Class] Namespace: [0×0 matlab.metadata.Namespace] Aliases: [0×1 string] RestrictsSubclassing: 0 PropertyList: [0×1 matlab.metadata.Property] MethodList: [145×1 matlab.metadata.Method] EventList: [0×1 matlab.metadata.Event] EnumerationMemberList: [6×1 matlab.metadata.EnumerationMember] SuperclassList: [1×1 matlab.metadata.Class]]
Each enumeration member is described by a
matlab.metadata.EnumerationMember
object that is contained in the
EnumerationMemberList
property. For example, the fourth element
in the EnumerationMemberList
array contains the
matlab.metadata.EnumerationMember
object for the member with the name
red
.
mc.EnumerationMemberList(4)
ans = EnumeratedValue with properties: Name: 'red' Description: '' DetailedDescription: '' Hidden: 1
To list the names of all hidden members, use the handle class findobj
method:
findobj(mc.EnumerationMemberList,'Hidden',true).Name
ans = 'red' ans = 'green' ans = 'blue'