classdef

Class definition keywords

Description

classdef ... end encloses a class definition.

Examples

collapse all

Use these keywords to define classes. For complete examples, see Sample Class Implementations.

classdef (Attributes) ClassName < SuperclassName
   properties (Attributes) 
      PropertyName
      PropertyName size class {validation functions}
   end 
   methods (Attributes) 
      function obj = methodName(obj,arg2,...)
         ...
      end
   end
   events (Attributes) 
      EventName
   end
end
classdef (Attributes) ClassName < SuperclassName
   enumeration
      EnumName
   end
end

properties, methods, events, and enumeration are also the names of MATLAB® functions used to query the respective class members for a given object or class name.

More About

collapse all

classdef

classdef is a keyword used to define MATLAB classes.

classdef ClassName begins the class definition and an end keyword terminates the classdef block. Only blank lines and comments can precede classdef. Enter a class definition in a file having the same name as the class, with a filename extension of .m. Valid class names begin with an alphabetic character, and can contain letters, numbers, or underscores.

classdef ClassName < SuperclassName1 & SuperclassName2 ... begins the class definition and specifies one or more superclasses. For more information on deriving classes from other classes, see Subclass Definition.

classdef (AttributeName1 = attributevalue, AttributeName2 = attributevalue, ...) ClassName begins the class definition and specifies optional class attributes. For a list of class attributes, see Class Attributes.

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 Class Files and Folders .

For more information on classes, see Classdef Block and Class Definition.

Properties

properties begins a property definition block; an end keyword terminates the properties block. Class definitions can contain multiple property definition blocks, each specifying different attribute settings that apply to the properties in that particular block.

For more information on properties, see Property Syntax.

Note

Properties cannot have the same name as the class.

Methods

methods begins a methods definition block; an end keyword terminates the methods block. This block contains functions that implement class methods. Class definitions can contain multiple method blocks, each specifying different attribute settings that apply to the methods in that particular block. It is possible to define method functions in separate files.

For more information on methods, see Methods in Class Design.

Events

events begins an events definition block; an end keyword terminates the events block. This block contains event names defined by the class. 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 events, see Events and Listeners Syntax.

Enumeration

enumeration begins an enumeration definition block; an end keyword terminates the enumeration block.

For more information on enumerations, see Enumerations.

Introduced in R2008a