Introduction to Object-Oriented Programming in MATLAB
Total Page:16
File Type:pdf, Size:1020Kb
Object Oriented & Event-Driven Programming with MATLAB Ameya Deoras © 2014 The MathWorks, Inc.1 Agenda . 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 2 Case Study: Portfolio Tracker 45.8 61.88 DD.N JNJ.N 45.7 61.86 45.6 61.84 45.5 61.82 61.8 06:05 06:06 06:07 06:06 06:07 49 -660 WMT.N Portfolio 48.9 48.8 -665 48.7 06:06 06:07 78.8 MMM.N -670 78.6 78.4 78.2 -675 06:04 06:05 06:06 06:07 06:02 06:03 06:04 06:05 06:06 06:07 . Subscribe to real-time quotes for 4 equities from Reuters service . Track real-time combined portfolio valueVisualize instrument & portfolio history graphically in real-time 3 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 4 Progression of Programming Techniques value Data variable structure Level of Abstraction / Sophistication function script command line Algorithm 5 Progression of Programming Techniques value Data variable structure (properties) Level of Abstraction / Sophistication class (methods) function script command line Algorithm 6 Object-Oriented Terminology . Class – Outline of an idea AKAM – Properties (data) GOOG YHOO MSFT – Methods (algorithms) ORCL An element of the set – object . Object – Specific example of a class – Instance Defined set – class Eg. Stock 7 From Structures to Objects Example: Class MarketInstrument with properties Ticker and Price. Operation Structure Class/Object Initialization, x = struct; x = MarketInstrument; Property Access x.Ticker = 'GOOG'; x.Ticker = 'GOOG'; x.Price = 550; x.Price = 550; Erroneous x.ticker = 'MSFT'; x.ticker = 'MSFT'; property name No public field ticker exists for class MarketInstrument Erroneous x.Price = 'blah'; x.Price = 'blah'; property value Error using MarketInstrument/set.Price Price must be a scalar numeric value 8 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 9 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 10 Unique Advantages of Classes & Objects . Property Access Control, Error Checking . Reference Semantics classdef Instrument >> x = Instrument; function myFun(y) >> x.Price = 12; y.Price = 15; >> myFun(x); >> x.Price ans = 12 11 Unique Advantages of Classes & Objects . Property Access Control, Error Checking . Reference Semantics classdef Instrument < handle >> x = Instrument; function myFun(y) >> x.Price = 12; y.Price = 15; >> myFun(x); >> x.Price ans = 15 12 Unique Advantages of Classes & Objects . Property Access Control, Error Checking . Reference Semantics . Overload default functions & operations – plot, disp, size – [ ], +, /, &, () >> x.Price = 12; function disp(obj) >> x disp(['My price is $'... num2str(obj.Price)]) My price is $12 13 Unique Advantages of Classes & Objects . Property Access Control, Error Checking . Reference Semantics . Overload default functions & operations . Automatic documentation 14 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 15 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…) 16 Agenda . 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 17 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 18 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) function setPrice(obj, newPr) obj.price = newPr; obj.price = newPr; display(obj) notify(obj,'priceChanged') end end addlistener(obj,... 'priceChanged', @obj.display) 19 Procedural vs Event-Driven Implementation Instrument Portfolio Price Instruments setPrice(newPrice) Value display() updateValue() Goal: When Instrument price changes, display new price and update value of Portfolios Event-Driven function setPrice(obj, newPr) obj.price = newPr; notify(obj,'priceChanged') end addlistener(instObj,'priceChanged', @instObj.display) addlistener(instObj,'priceChanged', @portObj.updateValue) 20 Sources of Events in MATLAB MATLAB MATLAB MATLAB timer Graphics Classes Object Datafeed External Trading Toolbox Interfaces Toolbox (Bloomberg, TT, IB, (Bloomberg, CQG) (COM, Java, .NET) Reuters) 22 Putting it Together: Portfolio Tracker 45.8 61.88 DD.N JNJ.N 45.7 61.86 45.6 61.84 45.5 61.82 61.8 06:05 06:06 06:07 06:06 06:07 49 -660 WMT.N Portfolio 48.9 48.8 -665 48.7 06:06 06:07 78.8 MMM.N -670 78.6 78.4 78.2 -675 06:04 06:05 06:06 06:07 06:02 06:03 06:04 06:05 06:06 06:07 . 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 23 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 24 Additional Resources 25 Questions and Answers 26 .