Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Object oriented programming in matlab

Richard E. Turner (rt60@nyu.edu) November 29th, 2011

Our goals in life...


unit tests test the code write bug free quickly version control write small units reuse the code clearly written easy to add/modify

object oriented

Jargon

object properties methods data-fields functions

Matlab simile: like a structure with pre-dened, xed elds

Jargon

object properties methods data-fields functions

un-normalised Gaussian natural parameters compute moments

Matlab simile: like a structure with pre-dened, xed elds

Jargon

class un-normalised Gaussian data-fields functions natural parameters compute moments

object properties methods

Matlab simile: like a structure with pre-dened, xed elds

Object oriented approach

Organises data and functions which belong together Abstracts so others can operate at the level of the objects (programming = creating abstractions) Data-forward thinking (rather than code forward) you think about the data rst. OO makes simpler programs more complicated, but complex ones more simple. Beware too much/forced abstraction. There is an overhead

How does it do this: Encapsulation and property access

object

properties

dependent properties

get methods

How does it do this: Encapsulation and property access

un-normalised Gaussian

object

natural parameters

moments

properties

dependent properties

moment computation

get methods

How does it do this: Inheritance and dynamic dispatch

un-normalised Gaussian
natural parameters compute moments

un-normalised Gamma
natural parameters compute moments

un-normalised isotropic Gaussian


natural parameters compute moments

dynamic dispatch e.g. exact moment computation methods

How does it do this: Inheritance and dynamic dispatch


un-normalised exponential family

inherit e.g. pdf methods un-normalised Gaussian


natural parameters compute moments

un-normalised Gaussian un-normalised isotropic Gaussian


natural parameters compute moments

un-normalised Gamma
natural parameters compute moments

dynamic dispatch e.g. exact moment computation methods

Example

Where to nd out more information


Lorens blog: When to use OO: http://blogs.mathworks.com/loren/2008/08/18/ when-to-create-classes-in-matlab/ Design considerations for OO: http://blogs.mathworks.com/loren/2011/ 03/08/common-design-considerations-for-object-properties/

Tutorial for matlab OO: http://www.mathworks.com/company/newsletters/articles/introduction-t html Software carpentry introduction: http://software-carpentry.org/4_0/oop/intro/

Online video tutorial: http://www.mathworks.com/products/demos/shipping/matlab/WhatsNewR200 ObjectOrientedProgramming.html

You might also like