MATLAB Classes

From ControlTheoryPro.com

Jump to: navigation, search
Symbol.gif
MATLAB Classes
Green carrot left.gif
MATLAB Modeling
Green carrot.jpg
In order to prevent spam, users must register before they can edit or create articles.


1 Introduction to MATLAB Classes

In MATLAB, like most programming languages, every value is a class. Typically the classes are double, char, float, single, cell, etc. (The class of a given value or object can be determined using the built-in MATLAB command class.)

For many repetitive tasks building your own classes makes sense. In my line of work we build a lot of very similar models and post-process most of them in a very similar manner. However, we may go a year or more between some of these proposal efforts meaning that many of the MATLAB implementation issues have been forgotten. As a result building the model and presenting the results usually took much longer than it should simply because we had to relearn a number of little gotchas. So recently I built a series of simple classes that encapsulate that knowledge and when we want the result we call the appropriate class method which already does all of the interpolation and data massaging that is required.

The following subsections will briefly touch the various parts of any class definition in MATLAB.

2 MATLAB Class Properties

The first thing you must define for any class object are the properties. The three primary property attributes are:

  • Public
  • Private
  • Hidden

There are several variations that can be applied.

Public properties are properties that show up when the object is displayed. These properties are viewable and settable from the command prompt. Public properties are what most people think of when they think of object properties.

Private properties are designated by

 properties (SetAccess = private)

and these properties are visible, read-only. The values of these properties can only be set using class properties though.

Finally Hidden properties are not visible from the class display method. If you know the property name you can both read and write to that property's value from the command prompt.

For more detailed information go here: MATLAB 2009a Class Property Attributes

3 MATLAB Class Methods

After the properties are defined the methods must be defined. There are 4 primary method attributes:

  • Public
  • Hidden
  • Sealed
  • Static

The Public and Hidden designations for methods are just like Public and Hidden properties. For Sealed methods a subclass cannot redefine the method. Static methods do not require an object argument.

For more detailed information go here: MATLAB 2009a Class Method Attributes

4 MATLAB Class Events

Events are an advanced topic where you create a listener for the event and action when the listener is triggered. The action can trigger other events and without careful design you can trigger events you don't intend to trigger.

For more detailed information go here: MATLAB 2009a Class Event Properties