classdef
Class definition keywords
Syntax
classdef (Attributes
)ClassName
<SuperclassNames
properties (Attributes
) ... end methods (Attributes
) ... end events (Attributes
) ... end enumeration ... end end
Description
classdef ... end
encloses a class definition. The first line
of the classdef
block has this syntax:
classdef (Attribute1 = value1, Attribute2 = value2,...) ClassName < SuperclassName1 & SuperclassName2 & ...
(
— Optional class attributes, specified as a comma-separated list of attribute names and their associated values. For example, this syntax defines an abstract class with a restricted list of allowed subclasses:Attribute1 = value1
,Attribute2 = value2
,...)classdef (Abstract = true, AllowedSubclasses = {ClassA, ClassB}) exampleClass
Attributes that take logical values can be used without an explicit value. In the previous example, specifying
Abstract
without an explicit value sets the attribute totrue
. For more information, see Class Attributes.
— Valid class names begin with an alphabetic character and can contain letters, numbers, or underscores. Save your class in a file with the same name as the class with a file extension ofClassName
.m
.
— List of superclasses, separated bySuperclassName1
&SuperclassName2
& ...&
characters. For more information on deriving classes from other classes, see Subclass Definition.
The classdef
block can include one or more of these class
member blocks:
Properties —
properties (
defines a property block. Class definitions can contain multiple property blocks, each specifying different attribute settings that apply to the properties in that particular block. For more information on property syntax, see Property Syntax.Attributes
) ... endMethods —
methods (
defines a method block. Class definitions can contain multiple method blocks, each specifying different attribute settings that apply to the methods in that particular block. For more information on method syntax, see Method Syntax.Attributes
) ... endEvents —
events (
defines an event block. Class definitions can contain multiple event blocks, each specifying different attribute settings that apply to the events in that particular block. For more information on event syntax, see Events and Listeners Syntax.Attributes
) ... endEnumeration —
enumeration ... end
defines an enumeration block. For more information on defining enumeration classes, see Define Enumeration Classes.
Examples
Tips
Only blank lines and comments can precede
classdef
.Class definition files can be in folders on the MATLAB® path or in class folders whose parent folder is on the MATLAB path. Class folder names begin with the
@
character followed by the class name (for example,@MyClass
). For more information on class folders, see Folders Containing Class Definitions.properties
,methods
,events
, andenumeration
are also the names of MATLAB functions used to query the respective class members for a given object or class name.Properties, events, and enumeration members cannot have the same name as their defining class.
You can define methods in files other than the main class file. For more information, see Methods in Separate Files.
Version History
Introduced in R2008a