MATLAB® software Version 7.6 introduces a new syntax for defining classes. This new syntax includes:
classdef keyword begins a block
of class-definitions code. An
end statement terminates
the class definition.
classdef code block,
events are also keywords delineating where
you define the respective class members.
It is not possible to create class hierarchies that mix classes
defined before Version 7.6 and current class definitions that use
classdef. Therefore, you cannot subclass
an old class to create a version of the new class.
For classes defined using the new
a class folder shadows all class folders that occur after it on the MATLAB path.
Classes defined in class folders must locate all class files in that
single folder. However, classes defined in class folders continue
to take precedence over functions and scripts having the same name,
even those functions and scripts that come before them on the path.
You do not need to define private folders in class folders in
Version 7.6. You can set the method's
Class constructor methods have two major differences. Class constructors:
Do not use the
Must call the superclass constructor only if you want to pass arguments to its constructor. Otherwise, no call to the superclass constructor is necessary.
Compare the following two
Stock class is a subclass of the
which requires arguments passed to its constructor.
Constructor Function Before Version 7.6
function s = Stock(description,num_shares,share_price) s.NumShares = num_shares; s.SharePrice = share_price; % Construct Asset object a = Asset(description,'stock',share_price*num_shares); % Use the class function to define the stock object s = class(s,'Stock',a);
Write the same
Stock class constructor as
shown here. Define the inheritance on the
and define the constructor within a
Constructor Function for Version 7.6
classdef Stock < Asset ... methods function s = Stock(description,num_shares,share_price) % Call superclass constructor to pass arguments s = s@Asset(description,'stock',share_price*num_shares); s.NumShares = num_shares; s.SharePrice = share_price; end % End of function end % End of methods block end % End of classdef block
Properties: How to Use Properties
Handle classes: Comparison of Handle and Value Classes
Events and listeners: Event and Listener Concepts
Class member attributes: Attribute Specification
Abstract classes: Abstract Classes
Dynamic properties: Dynamic Properties — Adding Properties to an Instance
Ability to subclass MATLAB built-in classes: Create Subclasses — Syntax and Techniques
The JIT/Accelerator supports objects defined only
by classes using
The MATLAB Version 7.6 implementation of classes uses different syntax from previous releases. However, classes written in previous versions continue to work. Most of the code you use to implement the methods is likely to remain the same, except where you take advantage of new features.
The following sections reimplement examples using the latest syntax. The original MATLAB Classes and Objects documentation implemented these same examples and provide a comparison of old and new syntax.