Você está na página 1de 44

Object-Oriented

Programming in MATLAB

Antti Lytynoja, M.Sc.


Application Engineer

2012 The MathWorks, Inc.1

Agenda
0910 1030

Object-oriented programming in
MATLAB
Classes, methods and properties
Encapsulation and inheritance
Events and listeners

1030 1050

BREAK

1050 1145

GUI development in MATLAB


Deploying applications
Database access with MATLAB
2

What is a program?
Data

x = 12
while (x < 100)
x = x+1
if (x == 23)
disp('Hello')
end
end

Code

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

Progression of Programming Techniques

Data

value
variable
structure

Level of Abstraction / Sophistication


Application Complexity
function
script
command line

Actions
4

Example: Sensor Array

Transmitting a signal
from a weather balloon

Locating the signal


with a sensor array

Computing the angle of


arrival for the signal (AoA)

Procedural Programming

Easy to learn
Minimal planning
No formal relationship
between data and functions
Every detail is exposed

Data and Actions to Implement

Wavelength

Number
Spacing

Location

Determine peaks
Compute FFT
Plot results

Reading
Frequency

Data

Synthesize measurements

Actions

Related Data and Actions

Balloon
Wavelength
Location
Signal
Location
Frequency
Wavelength
Frequency

Data

Number
Sensor
Spacing
Reading
Spacing
Reading
Number

Determine peaks

Sensor
Synthesize measurements
Compute
FFT peaks
Determine
Compute FFT Plot results
Plot results

Synthesize measurements

Actions

Grouping Related Data and Actions

Balloon
Location
Signal
Frequency
Wavelength

Data

Sensor
Synthesize measurements
Determine peaksSensor
Synthesize measurements
Compute FFT
Sensor
Determine peaks
Plot results Sensor
Reading
Compute FFT

Reading
Spacing
Plot results
Spacing
Number
Number

Class

Actions

Progression of Programming Techniques


Data

value
variable

structure

Level of Abstraction / Sophistication


Application Complexity

class

function

script
command line

Actions
10

Object-Oriented Terminology

Class
Blueprint of an idea
Properties (data)
Methods (actions)

Object
Specific example of a class
Instance

11

Demonstration: Building a Simple Class

Define a target class

Create the weather


balloon object

Use the object in place


of the structure

12

Objects

Easy to create

Manage their own data

Interchangeable
with a structure
No other code changes required
Properties behave similar to field names
Cant add fields arbitrarily

13

Demonstration:
Adding Methods to a Class

Add a custom constructor to target


class to overload default constructor

Add another method to the target


class to identify the balloon.

Integrate into the existing code

14

Objects with Methods

Have immediate access to


their own data (properties)

Allow you to overload


existing functions

Allow you to perform


custom actions at
creation and deletion

15

Taking properties and methods further

16

Encapsulation

Speed of
Light
Number of Towers

Synthesize
measurements

Tower Spacing

Sensor

Compute
FFT

Sensor Reading

Noise Ratio
etc.

Plot Results

Determine
Peaks
Compute AoA

17

Encapsulation

Speed
of
Separates the
interface
Light
from the implementation

Simplifies

Spacing
Tower
Becomes

Sensor Reading

Number of Towers
etc.

Number of Towers

Noise Ratio

Synthesize
object measurements
use

a building
block
Compute

Plot Results

Determine
Tower Spacing
Peaks

FFT

Sensor

Compute AoA

18

Demonstration: Applying Attributes

Control access
Access = public
Access = protected

Restrict modification
Constant
Dependent

19

Using a Class as a Building Block

The Red Baron


The Balloon

All Moving targets

All targets
20

Demonstration: Creating a Moving Target

Define a new class


MovingTarget

Inherit from the existing


class Target

Position
Move Source

Add a method

Use the moving target

Signal

21

Inheritance

Subclass substitutes
for the superclass

Allows re-envisioning
and re-implementing
the superclass

Builds on proven code

Allows inheriting from the base MATLAB classes

22

How does = work in MATLAB?


Round 1
>>
>>
>>
>>

a = 10000;
b = a;
b = 20000;
disp(a)

a) 10,000
b) 20,000
c) Something else

23

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,000
b) 20,000
c) Something else

24

B
Data

Data

Data

>> B = A;
A

A
Data

Data

25

Value Class

Handle Class

MATLAB default

Use: < handle

= copies data

= references data

data in workspace

handle in workspace

A
B

Data

Data

Data

26

Demonstration: Events and Listeners

Inherit from a handle class


Trigger an event to indicate
a change in object state
notify()

Make an object listen to


events
addlistener()

React to events
Callbacks

27

Events and Listeners

Events
Created in a handle object
events block in classdef
notify() triggers event

Listeners
Triggers callback function
addlistener()
Useable anywhere

28

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
Events and listeners from handle-class
Inherit from built-in MATLAB-classes

Extends the matrix-based language to objects

29

Agenda
0910 1030

Object-oriented programming in
MATLAB
Classes, methods and properties
Encapsulation and inheritance
Events and listeners

1030 1050
1050 1145

BREAK

GUI development in MATLAB


Deploying applications
Database access with MATLAB
30

Agenda
0910 1030

Object-oriented programming in
MATLAB
Classes, methods and properties
Encapsulation and inheritance
Events and listeners

1030 1050
1050 1145

BREAK

GUI development in MATLAB


Deploying applications
Database access with MATLAB
31

Demo:
Creating a GUI application

32

Deploying with MATLAB

MATLAB Compiler
MATLAB
Builder EX

.exe

.dll/
.so

Excel
Add-in

MATLAB
Builder JA

MATLAB Builder
NE

www

COM

33

Demo:
Creating a Standalone Executable

34

Introduction to MATLAB Compiler

Automatically converts your MATLAB programs into


stand-alone applications and C/C++ shared libraries
Encrypts your application

Supports full MATLAB language and


most toolboxes

Royalty-free deployment

Shared infrastructure with MATLAB:


Immediate support of MATLAB features
Speed of compiled application equivalent to speed in MATLAB
35

Working with MATLAB Compiler

3
Toolboxes

MATLAB
Desktop

1
2

End-User
Machine

MATLAB Compiler

36

Save Development Time And Costs by Sharing


Applications Royalty-Free

Suppliers

MATLAB User
Your Organization

Clients

Your Group

Field
engineers

37

Different Deployment Options

Very Flexible

Subset of language support

No code changes needed

C/C++ Source Code

For desktop deployment

Smaller memory footprint

Support for Graphics and (GUIs)

For embedded deployment

Royalty free distribution

Royalty free distribution

Desktop and Web

MATLAB Compiler

Source Code

MATLAB Coder

38

Agenda
0910 1030

Object-oriented programming in
MATLAB
Classes, methods and properties
Encapsulation and inheritance
Events and listeners

1030 1050
1050 1145

BREAK

GUI development in MATLAB


Deploying applications
Database access with MATLAB
39

Demo:
Database access with MATLAB

40

Summary

Object-oriented programming with MATLAB


Extend MATLAB with O-O principles
Standardize on data structures company-wide
Re-use code

Application deployment with MATLAB


Build GUI applications with MATLAB
Deploy as standalone or software components

Database access with MATLAB


Connect to any ODBC or JDBC compliant database
Use interactive tool or execute programmatically

41

Training
http://www.mathworks.se/training-schedule/
Dates

Oct

1st

Oct

4th

Course

3rd

MATLAB Fundamentals

5th

Simulink for System


and Algorithm Modeling

Language

Location

English

Sweden, Stockholm
(City)

English

Sweden, Stockholm
(City)

42

Thank You for Attending Todays Seminar

Products
www.mathworks.se

Upcoming events (seminars, webinars, etc.)


www.mathworks.se/events

Training
www.mathworks.se/training
Public and on-site courses
Ask us!

43

Questions?

44

Você também pode gostar