MATLAB® organizes class definition code into modular blocks, delimited by keywords. All keywords have an associated end
statement:
classdef...end
— Definition of all class components
properties...end
— Declaration of property names, specification of property attributes, assignment of default values
methods...end
— Declaration of method signatures, method attributes, and function code
events...end
— Declaration of event name and attributes
enumeration...end
— Declaration of enumeration members and enumeration values for enumeration classes.
properties
, methods
, events
, and enumeration
are keywords only within a classdef
block.
The classdef
block contains the class definition within a file that starts with the classdef
keyword and terminates with the end
keyword.
classdef (ClassAttributes) ClassName < SuperClass ... end
For example, this classdef
defines a class called MyClass
that subclasses the handle
class, but cannot be used to derive subclasses:
classdef (Sealed) MyClass < handle ... end
See, Classdef Block for more syntax information.
The properties
block (one for each unique set of attribute specifications) contains property definitions, including optional initial values. The properties block starts with the properties
keyword and terminates with the end
keyword.
classdef ClassName properties (PropertyAttributes) ... end ... end
For example, this class defines a property called Prop1
that has private access and has a default value equal to the output of the date
function.
classdef MyClass properties (SetAccess = private) Prop1 = date end ... end
See Property Definition for more information.
The methods
block (one for each unique set of attribute specifications) contains function definitions for the class methods. The methods block starts with the methods
keyword and terminates with the end
keyword.
classdef ClassName methods (MethodAttributes) ... end ... end
For example:
classdef MyClass methods (Access = private) function obj = myMethod(obj) ... end end end
See Define Class Methods and Functions for more information.
The events
block (one for each unique set of attribute specifications) contains the names of events that this class declares. The events block starts with the events
keyword and terminates with the end
keyword.
classdef ClassName events (EventAttributes) EventName end ... end
For example, this class defined an event called StateChange
with a ListenAccess
set to protected
:
classdef EventSource events (ListenAccess = protected) StateChanged end ... end
See Events and Listeners for more information.
A complete class definition contains any combination of properties, methods, and events 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 classes are specialized classes that define a fixed set of names representing a single type of value. Enumeration classes use an enumeration
block that contains the enumeration members defined by the class.
The enumeration block starts with the enumeration
keyword and terminates with the end
keyword.
classdef ClassName < SuperClass enumeration EnumerationMember end ... end
For example, this class defines two enumeration members that represent logical false
and true
:
classdef Boolean < logical enumeration No (0) Yes (1) end end
See, Define Enumeration Classes for more information.