USGS logo - Archived Page (no longer updated)

Matlab B.O.O.T. Camp

Basic Object Oriented Training

This article outlines practical steps for producing useful classes of objects in Matlab-5. The entire Matlab language can be adapted to the particular needs of custom-built objects, thereby allowing users to create new data-types that exhibit unconventional behaviors. An example class is included for downloading.


Example Class: "bigint"

As you work through this article, examine the M-files inside the "bigint / @bigint" sub-folder for illustrations of many of the concepts discussed here. The functions that are most tricky to construct are the "subs..." methods that process Matlab subscripts.


Class Candidates: Some Examples

Things that do arithmetic and logic.

  1. ND-arrays, vector-fields, tensors.
  2. Big integer, rational and multiple-precision numbers.
  3. Graphics.
  4. Time-series.
  5. Geographic information.
  6. Symbolic algebra, geometry, and logic.

Things that contain several pieces.

  1. Time-series (data + time).
  2. Rational-number (sign + numerator + denominator).
  3. MP-number (sign + signed-exponent + mantissa).
  4. Graphical interface (clickable components).
  5. Dictionary (names + meanings).
  6. Geographic entities (lat + lon + elev + info).
  7. File (name + status + buffer).
  8. Generic container (info + contents).
  9. Algebraic expression (parse-tree).

Things that use a common calling interface.

  1. Graphics: move, change, draw, erase, ...
  2. Files: open, read, write, close, ...
  3. Mathematics: +, *, ^, <, ...
  4. Containers: find, insert, sort, iterate, ...

Class Construction Step-By-Step

The following outline assumes that the reader already has some familiarity with Matlab-5, especially regarding the new data-types called "struct" and "cell". It is easy to philosophize that object-oriented programming is really more "attitude" than "technique", but somewhere along the line, someone has to actually put their hands on the keyboard and write the "class" software. That is where these steps come into play.

Create the class folder: "@classname" (required)

Build the constructor method: "classname.m" (required)

  1. Define the private data "struct" with empty fields.
  2. Create the object with class(theStruct, 'classname', ...).
  3. Initialize the private data fields, using access-methods.
  4. Additional processing. (optional)
  5. Return the object. (optional, but customary)

Build the access-methods: (optional)

  1. Use one access (set/get) method per private data-field.

Build the display-methods: (optional)

  1. display: Displays "name = value".
  2. disp: Displays "value" without name.

Build the operator-methods: (optional)