MATLAB 2009a Class Definition

From ControlTheoryPro.com

Jump to: navigation, search
Symbol.gif
MATLAB 2009a Class Definition
Green carrot left.gif
Property Attributes Method Attributes
Green carrot.jpg
In order to prevent spam, users must register before they can edit or create articles.


Contents

1 Introduction to MATLAB 2009a Classdef

In order to create a class you must first understand the syntax required. In MATLAB 2008a the syntax for defining classes became much easier. Prior to 2008a you had to define every set, get, and display method before you could really do much of anything else with the class. In 2008a default version of those methods were built-in. Now you can start creating the meat of the class immediately and tweak the set, get and display methods only as needed.

The new objects in MATLAB 2008a and beyond are much simpler to create and can be built, easily, in one file. However, there are often utility/support functions and private methods where it makes sense to create separate files for them.

The basic class definition syntax is (from MATLAB 2009a Help[1]):

classdef begins the class definition, which is terminated by an end key word. Only blank lines and comments can precede classdef. You must place a class definition in a file having the same name as the class, with a filename extension of .m. Class definition M-files can be in directories on the MATLAB path or in @ directories whose parent directory is on the MATLAB path.

2 Attribute Syntax[2]

The basic syntax required to create a class is as follows:

 classdef (attirbute-name = expression, ...) Classname
   
   properties (attribute-name = expression, ...)
     ...
   end % properties (attribute-name = expression, ...)
   
   methods (attribute-name = expression, ...)
     ...
   end % methods (attribute-name = expression, ...)
   
   events (attribute-name = expression, ...)
     ...
   end % events (attribute-name = expression, ...)
 end % classdef

Remember that there can be several properties, methods, and events declarations with different attributes (such as Public, Private, & Hidden). Then there can be multiple declarations of properties, methods, and events within each declaration (Public, Private & Hidden).

2.1 Table of Class Attributes[3]

Attribute Name

Class

Description

Hidden

logical

(default = false)

If set to true, the class does not appear

in the output of MATLAB commands or tools that display class

names.

InferiorClasses

cell

(default = {})

Use this attribute to establish a precedence relationship

among classes. Specify a cell array of meta.class objects

using the ? operator.

The built-in

classes double, single, char, logical, int64, uint64, int32, uint32, int16, uint16, int8, uint8, cell, struct, and function_handle are always inferior to user-defined

classes and do not show up in this list.

ConstructOnLoad

logical

(default = false)

If true, the class constructor is

called automatically when loading an object from a MAT-file. Therefore, the construction must be implemented so that calling it with no arguments

does not produce an error.

Sealed

logical

(default = false)

If true, the class can be not be subclassed.

3 Super Classes[4]

To define a class in terms of one or more other classes by specifying the superclasses on the classdef line:

 classdef class_name < superclass_name
   ...
 end

See Creating Subclasses for more information.

4 References

MATLAB 2009a Help

4.1 Notes

  1. The Classdef Block :: Class Definition-Syntax Reference
  2. The Classdef Block :: Class Definition-Syntax Reference
  3. Class Attributes :: Working with Classes
  4. The Classdef Block :: Class Definition-Syntax Reference


5 MATLAB Related Books