Вы находитесь на странице: 1из 27

# Introduction to Object-Oriented

Programming in MATLAB

Jamie Winter
Sr. Account Manager

Abhishek Gupta
Application Engineer

## © 2011 The MathWorks, Inc.1

Agenda

 Object-oriented programming

##  Basic object-oriented programming

syntax in MATLAB

 Classes in MATLAB

2
What is a program?
Data
x = 12
while (x < 100)
x = x+1
if (x == 23)
x = 12 disp('Hello')
while (x < 100) end
x = x+1 end
if (x == 23)
disp('Hello')
end Assignment
end Looping Test
Increment
Test to Act
Code Take Action
End
End

Actions 3
Progression of Programming Techniques

value Data
variable
structure

## Level of Abstraction / Sophistication

function
script
command line
Algorithm
4
Example: Sensor Array

 Transmitting a signal
from a weather balloon

##  Locating the signal

with a sensor array

##  Computing the angle of

arrival (AoA) for the signal

5
Procedural Programming

 Easy to learn
 Minimal planning

 There is no formal
relationship between
data and functions.
 Every detail is exposed.

6
Data and Actions to Implement

Wavelength

## Spacing Compute FFT

Location
Plot results

Synthesize measurements
Frequency

Data Actions

7
Related Data and Actions

## Balloon Number Determine peaks

Wavelength Sensor
 Location
 Synthesize measurements
Sensor
Spacing Compute FFT peaks
 Determine
Signal
Location  Reading  Compute FFT Plot results
 Frequency  Spacing  Plot results
Number
Synthesize measurements
Frequency

Data Actions

8
Grouping Related Data and Actions

Sensor
Balloon  Synthesize measurements
 Location  Determine peaksSensor
 Compute FFT  Synthesize measurements
Sensor  Determine peaks
 Plot results Sensor
Signal  Reading  Compute FFT
 Frequency  Spacing  Reading
 Plot results
 Wavelength  Number  Spacing
 Number

## Data Class Actions

9
Progression of Programming Techniques

value Data
variable
structure

(properties)
Level of Abstraction / Sophistication
class
(methods)

function
script
command line
Algorithm
10
Object-Oriented Terminology

 Class
– Outline of an idea
– Properties (data)
– Methods (algorithms)
An element of
the set – object
 Object
– Specific example of a class
– Instance Defined set – class

11
Agenda

 Object-oriented programming

##  Basic object-oriented programming

syntax in MATLAB

 Classes in MATLAB

12
Demonstration: Building a Simple Class

balloon object

##  Use the object in

place of the structure

13
Objects

##  Manage their own data

 Are interchangeable
with a structure
– No other code changes are required.
– Properties behave similar to field names.
– Fields can’t be added arbitrarily.

14
Demonstration:
 Start from a sensor class
with existing properties
Sensor
Synthesize measurements
Determine peaks
 Add a method to compute
Compute FFT
angle of arrival (AoA) Plot results

##  Integrate a sensor object

into the existing code

15
Objects with Methods

their own data (properties)

##  Allow you to overload

existing functions

##  Allow you to perform

custom actions at
creation and deletion

16
Agenda

 Object-oriented programming

##  Basic object-oriented programming

syntax in MATLAB

 Classes in MATLAB

17
Taking Methods and Properties Further

 Control access

 Create constants

##  Execute methods when properties change

18
Demonstration: Applying Attributes

 Control access
Access = public
Access = protected

 Restrict modification
Constant
Dependent

19
Encapsulation

Speed of
Light

## Number of Towers etc.

Synthesize
measurements
Plot Results

Determine
Tower Spacing Compute Peaks
FFT
Compute AoA
Sensor

20
Encapsulation

##  Separates the interface

from the implementation
Speed of
Light

Simplifies
 Number object
of Towers use Number of Towers
etc.
Synthesize
measurements
Plot Results
 Becomes a building block
Determine
Tower Spacing Compute Tower Spacing
Peaks
FFT
Compute AoA
Sensor

21
Using a Class as a Building Block

## The Red Baron

The Balloon

22
Demonstration:
Creating a Moving Target
 Define a new class
for moving blips

##  Inherit from the existing

Position
class for blips
Move Blip
Signal
 Use the moving blip

23
Inheritance

 Subclass substitutes
for the superclass

 Allows re-envisioning
and re-implementing
the superclass

##  Allows inheriting from the base MATLAB classes

24
Object-Oriented Programming in MATLAB

##  Class definition file describes object behavior

 Objects can substitute for structures
 Apply attributes for a clean interface
 Build on existing classes with inheritance

25