object oriented & event-driven programming with matlab · object-oriented programming in matlab...

25
1 © 2014 The MathWorks, Inc. Object Oriented & Event-Driven Programming with MATLAB Ameya Deoras

Upload: others

Post on 27-May-2020

24 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

1 © 2014 The MathWorks, Inc.

Object Oriented & Event-Driven

Programming with MATLAB

Ameya Deoras

Page 2: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

2

Object-oriented programming in MATLAB

– Classes in MATLAB

– Advantages of object oriented design

– Example: Designing a portfolio tracker

Events in MATLAB

– Event-driven programming fundamentals

– Writing event handlers

– Example: Building a real-time portfolio tracker

Agenda

Page 3: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

3

Case Study: Portfolio Tracker

06:05 06:06 06:07

45.5

45.6

45.7

45.8

06:06 06:0748.7

48.8

48.9

49

06:04 06:05 06:06 06:0778.2

78.4

78.6

78.8

06:06 06:0761.8

61.82

61.84

61.86

61.88

06:02 06:03 06:04 06:05 06:06 06:07-675

-670

-665

-660

DD.N

WMT.N

MMM.N

JNJ.N

Portfolio

Subscribe to real-time quotes for 4 equities from Reuters service

Track real-time combined portfolio valueVisualize instrument &

portfolio history graphically in real-time

Page 4: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

4

What is a program?

x = 12

while (x < 100)

x = x+1

if (x == 23)

disp('Hello')

end

end

Code

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

Actions

Page 5: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

5

Progression of Programming Techniques

Algorithm

Data

function

script

command line

value

variable

structure

Level of Abstraction / Sophistication

Page 6: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

6

Progression of Programming Techniques

Algorithm

Data

function

script

command line

value

variable

structure

Level of Abstraction / Sophistication class

(properties)

(methods)

Page 7: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

7

Object-Oriented Terminology

Class

– Outline of an idea

– Properties (data)

– Methods (algorithms)

Object

– Specific example of a class

– Instance

An element of

the set – object

Defined set – class

Eg. Stock

YHOO GOOG

AKAM

MSFT ORCL

Page 8: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

8

From Structures to Objects

Operation Structure Class/Object

Initialization,

Property Access

x = struct;

x.Ticker = 'GOOG';

x.Price = 550;

x = MarketInstrument;

x.Ticker = 'GOOG';

x.Price = 550;

Example: Class MarketInstrument with properties Ticker and Price.

Erroneous

property name

x.ticker = 'MSFT';

x.ticker = 'MSFT';

No public field ticker exists for class

MarketInstrument

Erroneous

property value

x.Price = 'blah'; x.Price = 'blah';

Error using MarketInstrument/set.Price

Price must be a scalar numeric value

Page 9: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

9

Unique Advantages of Classes & Objects

Property Access Control, Error Checking

– Specify default values

– Create constants

– Make values interdependent

– Execute methods when properties change

properties

Components % Components of portfolio

Quantity % Number of units of each component

end

properties (SetAccess = protected)

CurrentCompPrice % Up-to-date component price

end

properties (Dependent)

CurrentPrice % Up-to-date price of portfolio

end

Page 10: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

10

Unique Advantages of Classes & Objects

Property Access Control, Error Checking

– Specify default values

– Create constants

– Make values interdependent

– Execute methods when properties change

>> x.Price = 'twelve';

set.Price(obj, val)

assert(isnumeric(val))

Error using set.Price

Price must be a numeric value

Page 11: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

11

Unique Advantages of Classes & Objects

Property Access Control, Error Checking

Reference Semantics

>> x = Instrument;

>> x.Price = 12;

>> myFun(x);

>> x.Price

ans =

12

classdef Instrument

function myFun(y)

y.Price = 15;

Page 12: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

12

Unique Advantages of Classes & Objects

Property Access Control, Error Checking

Reference Semantics

>> x = Instrument;

>> x.Price = 12;

>> myFun(x);

>> x.Price

ans =

15

classdef Instrument < handle

function myFun(y)

y.Price = 15;

Page 13: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

13

Unique Advantages of Classes & Objects

Property Access Control, Error Checking

Reference Semantics

Overload default functions & operations

– plot, disp, size

– [ ], +, /, &, ()

>> x.Price = 12;

>> x

My price is $12

function disp(obj)

disp(['My price is $'...

num2str(obj.Price)])

Page 14: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

14

Unique Advantages of Classes & Objects

Property Access Control, Error Checking

Reference Semantics

Overload default functions & operations

Automatic documentation

Page 15: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

15

Unique Advantages of Classes & Objects

Property Access Control, Error Checking

Reference Semantics

Overload default functions & operations

Automatic documentation

Support for MATLAB-like constructs

– Dynamic allocation

– Vectorized access of properties

>> x(2) = MarketInstrument;

>> x(1).Price = 12;

>> x(2).Price = 15;

>> x(3).Price = 20;

>> y = [x.Price]

y =

12 15 20

Page 16: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

16

Unique Advantages of Classes & Objects

Property Access Control, Error Checking

Reference Semantics

Overload default functions & operations

– plot, disp, size

– [ ], +, /, &,

Automatic documentation

Support for MATLAB-like constructs

– Dynamic allocation

– Vectorized access of properties

Plus all the benefits of OOP in general (encapsulation, inheritance…)

Page 17: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

17

Object-oriented programming in MATLAB

– Classes in MATLAB

– Advantages of object oriented design

– Example: Designing a portfolio tracker

Events in MATLAB

– Event-driven programming fundamentals

– Writing event handlers

– Example: Building a real-time portfolio tracker

Agenda

Page 18: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

18

Introduction to Event-Driven Programming

! ! !

time

Terminology

Event handler/Callback: Function called to respond to event

Listener: Object that monitors for a specific event & calls handler

Event data: Data associated with event

Page 19: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

19

Procedural vs Event-Driven Implementation

Instrument

Price

setPrice(newPrice)

display()

Goal: When Instrument price changes, display new price

Procedural Event-Driven

function setPrice(obj, newPr)

obj.price = newPr;

display(obj)

end

function setPrice(obj, newPr)

obj.price = newPr;

notify(obj,'priceChanged')

end

addlistener(obj,...

'priceChanged', @obj.display)

Page 20: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

20

Instrument

Price

setPrice(newPrice)

display()

Goal: When Instrument price changes, display new price and update

value of Portfolios

Portfolio

Instruments

Value

updateValue()

Procedural vs Event-Driven Implementation

Event-Driven

function setPrice(obj, newPr)

obj.price = newPr;

notify(obj,'priceChanged')

end

addlistener(instObj,'priceChanged', @instObj.display)

addlistener(instObj,'priceChanged', @portObj.updateValue)

Page 21: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

22

Sources of Events in MATLAB

MATLAB Graphics

MATLAB Classes

MATLAB timer Object

External Interfaces

(COM, Java, .NET)

Datafeed Toolbox

(Bloomberg, Reuters)

Trading Toolbox (Bloomberg, TT, IB,

CQG)

Page 22: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

23

Putting it Together: Portfolio Tracker

06:05 06:06 06:07

45.5

45.6

45.7

45.8

06:06 06:0748.7

48.8

48.9

49

06:04 06:05 06:06 06:0778.2

78.4

78.6

78.8

06:06 06:0761.8

61.82

61.84

61.86

61.88

06:02 06:03 06:04 06:05 06:06 06:07-675

-670

-665

-660

DD.N

WMT.N

MMM.N

JNJ.N

Portfolio

Subscribe to real-time quotes for 4 equities (MarketInstrument)

Track real-time combined portfolio value (MarketPortfolio)

Handle market events from Reuters service

Visualize instrument & portfolio history graphically in real-time

Page 23: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

24

Benefits of OOP in MATLAB

Separate interface from implementation

– More checks and balances and control over how your code is used

Manage complex data types with familiar interfaces

– Custom objects that override default operators and functions

Pass by reference

– Maintain one true copy of your data

Use event-driven programming

– Simplify design of complex real-time applications

Page 24: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

25

Additional Resources

Page 25: Object Oriented & Event-Driven Programming with MATLAB · Object-oriented programming in MATLAB –Classes in MATLAB –Advantages of object oriented design –Example: Designing

26

Questions and Answers