Main Content

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 & ...

  • (Attribute1 = value1, Attribute2 = value2,...) — 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:

    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 to true. For more information, see Class Attributes.

  • ClassName — 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 of .m.

  • SuperclassName1 & SuperclassName2 & ... — List of superclasses, separated by & 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 (Attributes) ... end 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.

  • Methods methods (Attributes) ... end 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.

  • Events events (Attributes) ... end 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.

  • Enumeration enumeration ... end defines an enumeration block. For more information on defining enumeration classes, see Define Enumeration Classes.

Examples

collapse all

The Motor class stores the current speed of an electric motor and provides basic functionality for starting and stopping the motor. The class inherits from the ElectricVehicleComponent superclass and includes a property block and a method block.

classdef Motor < ElectricVehicleComponent
   
    properties
        CurrentSpeed = 0
        SpeedRange = [0, 180]
    end

    methods
        function motor = start(motor,speed)
            arguments
                motor (1,1) Motor
                speed (1,1) {mustBeReal, mustBeNonnegative}
            end
            if motor.CurrentSpeed > 0
                error("Motor:start:MotorAlreadyRunning",...
                    "Cannot start a motor that is already running.")
            end
            motor.CurrentSpeed = speed;   
        end
        
        function motor = stop(motor)
            if motor.CurrentSpeed == 0
                error("Motor:start:MotorNotRunning",...
                    "Cannot stop a motor that is not running.")
            end
            motor.CurrentSpeed = 0;
        end
    end
end

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, and enumeration 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