This hands-on how-to demonstrates how to use object-oriented programming features of Matlab 2008a to model C++ digital signal processing solutions.
By James Metzger, SPARTA, Inc.
Hosting such architecture in prior editions of MATLAB was possible but a kludge in the extreme (I know because I did it). Although there was barely a nod toward OOP, the age-old policy of passing by value made it particularly difficult to model a system of interacting objects. Now with the recent release of 2008a, MATLAB effectively provides the tools to express signal processing and other scientific algorithms in an object-oriented design.
New features
Thankfully, pass-by-reference is provided as part of the new object-oriented programming features of MATLAB 2008a. This "reference behavior" is done through the use of the MATLAB handle class. This referencing, taken for granted in other languages, is probably the most critical of the new features.
A class can be defined using the new keyword classdef definition-block. Within this block one can create one or more properties definition-blocks for member data declarations as well as one or more methods definition-blocks for member functions. All definition-blocks start with one of the aforementioned keywords and terminate with the all-popular end familiar to those that use switch and if-else statements in traditional MATLAB functions. Like traditional MATLAB code, class declarations and definitions are inscribed in M-Files (in other words, text files using the .m extension)
Attributes of classes, methods and properties can be set in a parenthetical list after the keyword. In this way, classes can be given custom attributes. As one would expect, properties can be abstract, constant, public, protected, private and more. Likewise, methods can be declared virtual, public, private, and static--notions common in languages like Java and C++.
One of the most interesting features of the language is a built-in listener-event model--and this is where reference behavior is crucial. Like the other keywords, an events definition block will declare any number of events that can also be given special attributes. Listeners can be registered using the addlistener function which declares callbacks to be executed once the event is dispatched. Conveniently, these callbacks can also be class methods thereby providing the infrastructure for the design pattern of signal-processing objects described earlier. Modules synthesizing events can call the notify function to agnostically dispatch an event to a system of listeners.