catalogue
Class component
Class building block
MATLAB ® Organize the class definition code into modular code blocks separated by keywords. All keywords have associated end statements:
-
classdef...end - definition of all class components
-
properties...end - attribute name declaration, attribute property setting, default value assignment
-
methods...end - declaration of method signature, method properties, and function code
-
events...end - declaration of event name and attribute
-
enumeration...end - declaration of enumeration members and enumeration values of the enumeration class.
properties, methods, events, and enumeration are just keywords in the classdef code block.
Class definition code block
The classdef code block contains the class definitions in the file, starting with the classdef keyword and ending with the end keyword.
classdef (ClassAttributes) ClassName < SuperClass ... end
For example, the following classdef defines a class named MyClass, which is a subclass of the handle class, but it cannot be used to derive subclasses:
classdef (Sealed) MyClass < handle ... end
Attribute code block
The properties code block (one for each unique property set) contains property definitions, including optional initial values. The attribute code block starts with the properties keyword and ends with the end keyword.
classdef ClassName properties (PropertyAttributes) ... end ... end
For example, the following class defines an attribute named Prop1, which has private access rights, and the default value is equal to the output of the date function.
classdef MyClass properties (SetAccess = private) Prop1 = date end ... end
Method code block
The methods code block (one for each unique attribute set) contains the function definition of the class method. The method code block starts with the methods keyword and ends with the end keyword.
classdef ClassName methods (MethodAttributes) ... end ... end
For example:
classdef MyClass methods (Access = private) function obj = myMethod(obj) ... end end end
Event code block
The events code block (one for each unique attribute set) contains the name of the event declared by this class. The event code block starts with the events keyword and ends with the end keyword.
classdef ClassName events (EventAttributes) EventName end ... end
For example, the following class defines an event named StateChange, and ListenAccess is set to protected:
classdef EventSource events (ListenAccess = protected) StateChanged end ... end
Complete class
The complete class definition contains any combination of attributes, methods, and event code blocks.
classdef (Sealed) MyClass < handle properties (SetAccess = private) Prop1 = datenum(date) end properties Prop2 end methods function obj = MyClass(x) obj.Prop2 = x; end end methods (Access = {?MyOtherClass}) function d = myMethod(obj) d = obj.Prop1 + x; end end events (ListenAccess = protected) StateChanged end end
Enumeration class
An enumeration class is a specialized class that defines a fixed set of names that represent a single type value. The enumeration class uses the enumeration code block to contain the enumeration members defined by the class.
The enumeration code block starts with the # enumeration # keyword and ends with the # end # keyword.
classdef ClassName < SuperClass enumeration EnumerationMember end ... end
For example, the following class defines two enumeration members, representing the logical values false and true respectively:
classdef Boolean < logical enumeration No (0) Yes (1) end end