Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ......

35
© 2008 The MathWorks, Inc. ® ® Introduction to Object-Oriented Programming in MATLAB ® Jos Martin Principle Software Engineer [email protected]

Transcript of Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ......

Page 1: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

©20

08 T

he M

athW

orks

, Inc

.

® ®

Introduction to Object-Oriented Programming in MATLAB ®

Jos Martin

Principle Software Engineer

[email protected]

Page 2: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

2

® ®

Goals

� Object-oriented programming

� Basic syntax in MATLAB®

� The MATLAB class system

Page 3: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

3

® ®

What is a program?

x = 12

while (x < 100)

x = x+1

if ( x == 23)

disp(‘Hello’)

end

end

Data

x = 12

while (x < 100 )

x = x+1

if ( x == 23)

disp( ‘Hello’ )

end

end

Assignment

Looping Test

Increment

Test to Act

Take Action

End

End

AlgorithmCode

Page 4: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

4

® ®

Progression of Programming Techniques

Algorithm

Data

functionscript

command line

literal

variable

structure

Level of Abstraction / Sophistication

Page 5: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

5

® ®

Example: Sensor Array

� Transmitting a signalfrom a weather balloon

� Locating the signalwith a sensor array

� Computing the angle ofarrival for the signal (AoA)

Page 6: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

6

® ®

Procedural Programming

� Easy to learn� Minimal planning

� No formal relationshipbetween data and functions

� Every detail is exposed

Page 7: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

7

® ®

Data and Actions to Implement

ActionsData

Compute FFT

Plot results

Determine peaks

Synthesize measurements

LocationSpacing

Reading

WavelengthNumber

Frequency

Page 8: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

8

® ®

Target

� Location

Signal

� Frequency

� Wavelength

Sensor

� Reading

� Spacing

� Number

Related Data and Actions

ActionsData

Sensor

� Synthesize measurements

� Determine peaks

� Compute FFT

� Plot results

LocationSpacing

Reading

WavelengthNumber

Frequency

Compute FFT

Plot results

Determine peaks

Synthesize measurements

Page 9: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

9

® ®

Target

� Location

Signal

� Frequency

� Wavelength

Grouping Related Items

ActionsData

Sensor

� Synthesize measurements

� Determine peaks

� Compute FFT

� Plot results

Sensor

� Synthesize measurements

� Determine peaks

� Compute FFT

� Plot results

Class

Sensor

� Reading

� Spacing

� Number

Sensor

� Reading

� Spacing

� Number

Page 10: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

10

® ®

Progression of Programming Techniques

Algorithm

Data

functionscript

command line

literal

variable

structure

class

Page 11: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

11

® ®

Object-Oriented Terminology

� Class� Blueprint of an idea� Properties (data)

� Methods (algorithms)

� Object� Specific example of a class� Instance

An element ofthe set – object

Defined set – class

Page 12: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

12

® ®

Goals

� Object-oriented programming

� Basic syntax in MATLAB®

� The MATLAB class system

Page 13: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

13

® ®

Demonstration: Building a Simple Class

� Define a target class

� Create the weatherballoon object

� Use the object in placeof the structure

Page 14: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

14

® ®

Objects

� Easy to create

� Manage their own data

� Interchangeablewith a structure� No other code

changes required

� Properties behave similar to field names

� Can’t add fields arbitrarily

Page 15: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

15

® ®

Sensor

� Synthesize measurements

� Determine peaks

� Compute FFT

� Plot results

Demonstration:Adding Methods to a Class

� Start from a sensor classwith existing properties

� Add a method to computeangle of arrival (AoA)

� Integrate a sensor objectinto the existing code

Page 16: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

16

® ®

Objects with Methods

� Have immediate access totheir own data (properties)

� Allow you to overloadexisting functions

� Allow you to performcustom actions atcreation and deletion

Page 17: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

17

® ®

Goals

� Object-oriented programming

� Basic syntax in MATLAB®

� The MATLAB class system

Page 18: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

18

® ®

The MATLAB Class System

� Designed to ‘feel’ like MATLAB

� Incorporates matrix indexing>> x = 2*obj.data(1:end);

� Inherent overloadingvarargout = obj.function(varargin)

� Works like an object-oriented language� Encapsulation, inheritance, polymorphism, etc.

Page 19: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

19

® ®

Taking Methods and Properties Further

� Control access

� Create constants

� Make valuesinterdependent

� Execute methods whenproperties change

External Methods

� Plot results

� Compute AoA

Internal Methods

� Synthesizemeasurements

� Determine peaks

� Compute FFT

External Data

� Reading

� Spacing

� Number

Internal Data

� Speed of light

� Noise ratio

� etc.

Page 20: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

20

® ®

Demonstration: Applying Attributes

� Control accessAccess = public

Access = protected

� Restrict modification Constant

Dependent

Page 21: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

21

® ®

Encapsulation

Sensor

Plot results

Synthesize measurements

Determine Peaks

Compute FFT

Speed of Light

etc.

Noise Ratio

Sensor

Plot results

Compute AoA

Number of Towers

Sensor Reading

Tower Spacing

Page 22: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

22

® ®

Sensor

Plot results

Synthesize measurements

Determine Peaks

Compute FFT

Speed of Light

etc.

Noise Ratio

Plot results

Compute AoA

Number of Towers

Sensor Reading

Tower Spacing

Encapsulation

� Separates the interfacefrom the implementation

� Simplifies object use

� Becomes a building block

Plot results

Compute AoA

Page 23: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

23

® ®

Assignment

Looping Test

Increment

Test to Act

Take Action

End

End

Using an Object as a Building Block

Page 24: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

24

® ®

Using a Class as a Building Block

All Targets

The BalloonAll Moving Targets

The Red Baron

Page 25: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

25

® ®

Demonstration: Creating a Moving Target

� Define a new classmoving target

� Inherit from the existingclass target

� Add a method

� Use the moving target

Move Target

Signal

Position

Page 26: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

26

® ®

Inheritance

� Subclass substitutesfor the superclass

� Allows re-envisioning andre-implementing the superclass

� Builds on proven code

� Allows inheriting from the base MATLAB classes

Page 27: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

27

® ®

How does ‘=’ work in MATLAB?Round 1

>> a = 10000;

>> b = a;

>> b = 20000;

>> disp(a)

a) 10,000b) 20,000c) Something elsed) No idea

Page 28: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

28

® ®

How does ‘=’ work in MATLAB?Round 2

>> a = analoginput('winsound'); addchannel(a,1);

>> a.SampleRate = 10000;

>> b = a;

>> b.SampleRate = 20000;

>> disp(a.SampleRate)

a) 10,000b) 20,000c) Something elsed) No idea

Page 29: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

29

® ®

Data

Data

A

A

Data

A

B

Data

Data

A

B

>> B = A;

Page 30: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

30

® ®

‘=’ references data‘=’ copies data

handle in workspacedata in workspace

Use: < handleMATLAB default

Value Class Handle Class

Data

Data

A

B Data

A

B

Page 31: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

31

® ®

Optional Demonstration: Using Events

� Events� Created in a handle object� events block in classdef

� notify(…) triggers event

� Listeners� Triggers call back function� addlistener(…)

� Useable anywhere

Page 32: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

32

® ®

Events and Listeners

� Uses technology related to� preSet

� postSet

� preGet

� postGet

� Gives the ability totrigger action

� Anything can listen to an observable object

Page 33: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

33

® ®

The MATLAB Class System

� Class definition file describes object behavior� Objects can substitute for structures� Apply attributes for a clean interface� Build on existing classes with inheritance

Extends the matrix-based language to objects

Page 34: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

34

® ®

Additional Resources

Page 35: Introduction to Object-Oriented Programming in … to Object-Oriented Programming in MATLAB ... disp( ‘Hello’) end end Assignment Looping Test

35

® ®

Questions and Answers