ele532 matlabtutorial full
TRANSCRIPT
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 1/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
1
ELE 532 – Signals and SystemsFall 2007
MATLAB Tutorial
Raymond PhanDistributed Multimedia Computing Research (DMCR) Lab
Ryerson University – EPH 237
http://www.ee.ryerson.ca/~rphan/ele532/MATLABTutorial.ppt
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 2/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
2
Outline of Tutorial – (1)
• Introduction
• Getting Started
• Variables and Starting Basics• Vectors and Matrices
• Basic Operations
• Script Files• Function Script Files
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 3/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
3
Outline of Tutorial – (2)
• Flow Control
• Basic Graphics Commands
• Other Useful Commands• Final Words
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 4/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
4
Introduction – (1)
• So… what the hell is MATLAB and what‟s it allabout? – MATLAB: MATrix LABoratory
• Created in 1970 by a dude named Cleve Moler• Was (and still is) used extensively at Stanford
University and the University of New Mexico – Why? To make calculating the following things a lot
easier!• Matrix Theory
• Linear Algebra
• Numerical Analysis
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 5/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
5
Introduction – (2)
• MATLAB is selected as a numerical analysis toolover languages like C and Java because: – Very EASY programming language
– Powerful graphics capabilities
– Very sleek and interactive interface – Great for general scientific and engineeringcomputation
• Later in your courses, you‟re going to start to usethis heavily, especially in: – ELE 639: Control Systems
– ELE 792: Digital Signal Processing
– … any signal processing and controls course in 4th year
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 6/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
6
Outline of Tutorial – (1)
• Introduction
• Getting Started
• Variables and Starting Basics• Vectors and Matrices
• Basic Operations
• Script Files• Function Script Files
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 7/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
7
Getting Started – (1)
• Where can I find and use MATLAB?
• Method #1: On the EE undergraduate
network labs:
– ENG 406, 407, 408 and 409
• Log onto an EE undergraduate terminal
• A) Go to Applications – Accessories – Terminal
and type in „matlab‟ (without the quotes) • B) Go to Applications – Math – MATLAB 2007a
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 8/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
8
Getting Started – (2)
• Method #2: If you don‟t feel like using thecomputers at school, you can install your owncopy of MATLAB on your PC or laptop – There are many ways to obtain your own copy:
• Buy a student version at the Ryerson Bookstore
• Obtain a trial version online via: http://www.mathworks.com • “Borrow” from a friend
– Version of MATLAB needed for these labs: MATLAB7.0 and up
– NOTE!: You MUST have the Java Runtime
Environment (JRE) installed on your system• At LEAST 5.0 and up
– MATLAB uses the JRE as a backbone to run thewhole application
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 9/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
9
Getting Started – (3)• What happens next!?
– MATLAB Interface: >> means it‟s ready for input from you
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 10/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
10
Outline of Tutorial – (1)
• Introduction
• Getting Started
• Variables and Starting Basics• Vectors and Matrices
• Basic Operations
• Script Files• Function Script Files
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 11/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
11
Variables and Basic Commands –
(1)
• One GREAT thing about MATLAB:
– MATLAB is a programming language that isdynamically typed… what does this mean?
– You can declare variables and initialize themwithout specifying what type they are
• MATLAB automatically figures this out for you, oryou can choose to manually override the type
– Example:• C or Java way: int nikhil = 1, doublejenny = 2
• MATLAB way: nikhil = 1, jenny = 2
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 12/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
12
Variables and Basic Commands –
(2)• When you want to assign something to a
variable, use the = sign
• When you assign something to a variable,MATLAB initializes & automatically declares it
• Guidelines for variable names: – All must be single words, no spaces
– Must begin with a letter, numbers or the underscorecharacter ( _ )
– Variable names are case sensitive• i.e nikhil is NOT the same as Nikhil
• i.e muffin is NOT the same as mUFfin
– Names can be up to 19 characters in length
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 13/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
13
Variables and Basic Commands –
(3)
• Some valid variable names:– voltage
– valueOfR1
– Ron_and_Mauro– _Alan2007_
• Some invalid variable names (why are theseinvalid?):
– 123– value of R1
– 3v
– X#*()$#$!!!
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 14/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
14
Variables and Basic Commands –
(4)
• Left panel: Current Directory / Workspace
– A) Shows you directory structure to access working
directory (more on this later)
– B) Shows you all of the variables that have beencreated and can be accessed
• Right: Command Prompt
– Enter commands and variable declarations here
– Commands without a semicolon ( ; ) echo yourcommand to screen
– Commands with a semicolon suppress that output
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 15/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
15
Variables and Basic Commands –
(5)• Can enter commands either:
– One at a time: The end of each command, pressENTER (carriage return)
– Multiple commands in one line:• Suppress echoing: Use semicolons to separate each
command on the line• Enable echoing: Use commas ( , ) to separate each
command on the line
• Typing in a variable by itself and pressingENTER will redisplay the variable
• Entering a value, pressing ENTER, and not assigning it to anything, the value will beautomatically assigned to a variable called ans (answer)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 16/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
16
Variables and Basic Commands –
(6)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 17/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
17
Variables and Basic Commands –
(7)
• who command: Shows you all of thevariables created by you
– You can also check the workspace as well
• clear command: Clears all of the variablesshown in the workspace, and you startfrom scratch
• clc command: Flushes the commandprompt
– Variables will still be in the workspace, but itclears the command prompt screen
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 18/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
18
Variables and Basic Commands –
(8)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 19/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
19
Variables and Basic Commands –
(9)
• Can also declare complex numbers too:
– Can add, subtract, multiply and divide
• You can use i or j to declare complex
numbers• Of course… you can also add, subtract,
multiply and divide normal numbers too!
– Too lazy to make a slide for it – However, we‟ll get into addition, subtraction,
multiplication and division in another way later
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 20/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
20
Variables and Basic Commands –
(10)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 21/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
21
Variables and Basic Commands –
(11)
• Command History window: Used to keep trackof the commands you ran recently
• You can alsodouble click on
any of the commandsto re-run them again
• You can also pressthe up & down keys
to cycle throughthe commands aswell in the commandprompt
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 22/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
22
Outline of Tutorial – (1)
• Introduction
• Getting Started
• Variables and Starting Basics• Vectors and Matrices
• Basic Operations
• Script Files• Function Script Files
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 23/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
23
Vectors and Matrices – (1)
• Unless otherwise defined, MATLAB treatsALL variables as 2D matrices… how isthis possible?
– Arrays and Vectors: N x 1 or 1 x N matrix
– Single value: 1 x 1 matrix
• Why does MATLAB decide to handle it thisway?
– You‟ll see later that handling variables asmatrices makes things A LOT faster andeasier to work with
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 24/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
24
Vectors and Matrices – (2)• How do you declare a vector / array in
MATLAB? – C or Java way: int a[4] = {1, 2, 3, 4};
– MATLAB way: • a = [1 2 3 4] – row vector
– Spaces mean to move to the next column
• a = [1 2 3 4].’ – (.‟ operator means to transpose avector) - column vector
• a = [1;2;3;4] - column vector – Semicolon means to move to the next row
• You do not have to specify how big the vector isfirst before you make it – Beauty of dynamically typed languages!
– MATLAB automatically figures out how big it is andyou go from there
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 25/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
25
Vectors and Matrices – (3)• How do I access elements in a vector /
array? – C or Java way:
•int jenny = a[0];
– MATLAB way:•jenny = a(1);
• NOTE!:
– No square brackets when accessing anelement! Use round brackets!
– Elements do not start at index 0, they start atindex 1!
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 26/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
26
Vector and Matrices – (4)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 27/142
ELE 532 –
Signals and SystemsMATLAB Tutorial
Friday, September 14th, 2007
27
Vector and Matrices – (5)
• How do I create a matrix in MATLAB? – C or Java way: int a[4][4] = {{1, 2,3, 4}, {5, 6, 7, 8}, {9, 10, 11,
12}, {13, 14, 15, 16}}; – MATLAB way:
• #1: a = [1 2 3 4; 5 6 7 8; 9 10 11 12;13 14 15 16];
• #2: a = [1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16];
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 28/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
28
Vector and Matrices – (6)
• How do I access elements in a matrix? – C or Java way:
• int alan = a[2][3];
– MATLAB way:• alan = a(3,4);
• What‟s the difference here? – No separate brackets for each dimension
– Comma is used to separate the dimensions
– All indices to access arrays are offset by 1!
– Remember: 1st parameter is the row, 2nd parameter isthe column
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 29/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
29
Vector and Matrices – (7)
• Here‟s something to mess you up… how do I
access a range of values in a matrix?
• Suppose I had a matrix already created called
ray – How do I get all of the elements in the 1st row?
– C or Java way:• int i;
for(i = 0; i < 4; i++)ray[i] = a[0][i];
• This is a pain in the butt!... There‟s gotta be an easier way to
do this!
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 30/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
30
Vectors and Matrices – (8)• MATLAB way:
– ray = a(1, 1:4);– ray = a(1, :);
• What‟s the difference here?! – NO for loop!
– The colon ( : ) operator is used to access a range ofvalues• There is a more general use for this, but we‟ll get into this later
– 1 : 4 means a range from 1 through 4 for a dimension
– : by itself means give me all possible values in a
dimension – Doing 1 : 4 in the 2nd parameter means give me
columns 1 through 4
– Doing : in the 2nd parameter means give me all of thecolumns!
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 31/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
31
Vectors and Matrices – (9)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 32/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
32
Vectors and Matrices – (10)• Some more examples:
– sally = a(2,3);
– sally = a(1:3,3:4);
– sally = a(:, 1:3);
– sally = a(2:4,:);
• 1st line: Access 2nd row, 3rd column element, and assign
it to sally • 2nd line: Get a matrix with elements between rows 1 – 3,
and columns 3 – 4 and assign this to sally
• 3rd line: Get a matrix with elements between columns 1 – 3 and give me every possible row, and assign this tosally
• 4th line: Get a matrix with elements between rows 2 – 4and give me every possible column, and assign this tosally
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 33/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
33
Vectors and Matrices – (11)
• Here‟s a curve ball!
– Joe = a(:,:);
– Joe = a;
• What does this mean? Copy the entire matrix, a, and assign it to Joe
• You can also do the 2nd line too. It‟s
exactly the same meaning
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 34/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
34
Vectors and Matrices – (12)
• Example Time!
• 1) How do we define M in MATLAB
syntax?• 2) How do we execute a), b), c) and d)?
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 35/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
35
Vectors and Matrices – (13)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 36/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
36
Vectors and Matrices – (14)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 37/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
37
Vectors and Matrices – (15)
• Example #2!
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 38/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
38
Vectors and Matrices – (16)
• In other words, with matrices A, B and C,
make me a matrix that looks like this!
• What do we need to do? – How do we define matrices A, B and C?
– How do we create M?
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 39/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
39
Vectors and Matrices – (17)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 40/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
40
Vectors and Matrices – (18)
• MATLAB has a really cool way of making
vectors / arrays where consecutive
elements are uniformly spaced
• Example:
– Ray = 0 : 0.1 : 1.0;
• This generates a vector / array with 11
elements, such that
– Ray = [0 0.1 0.2 0.3 … 1.0];
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 41/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
41
Vectors and Matrices – (19)
• General Form:– new_array = first_value : increment :last_value
– Make note of the colons ( : )!
• first_value: The first value in the new vector/ array
• last_value: The last value in the new vector /array
• increment: The step size
– If you don‟t include this value, it is automaticallyassumed to be 1
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 42/142
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 43/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
43
Vectors and Matrices – (22)
• 3rd line: Creates an 10 element vector– ron = [1 2 3 … 9 10];
• 4th line: Creates a 8 element vector
– mauro = [2.0 1.8 … 0.6 0.4];
• Pretty easy don‟t you think!?
• Remember how I told you about that colon
operator?... Well, here you go!• To do this in C and Java, it requires a bit
more work.
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 44/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
44
Vectors and Matrices – (23)
• Some useful matrix and vector / array
commands
– eye(n): Creates an n x n identity matrix
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 45/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
45
Vectors and Matrices – (24)
• Some useful matrix and vector / array
commands
– ones(n,m): Creates an n x m matrix full of ones
– ones(1,n) or ones(n,1): Creates an array /
vector that has n elements, full of ones
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 46/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
46
Vectors and Matrices – (25)
• Some useful matrix and vector / array
commands
– zeros(n,m): Creates an n x m matrix full of zeros
– zeros(1,n) or zeros(n,1): Creates an array /
vector that has n elements, full of zeros
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 47/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
47
Vectors and Matrices – (26)
• Last important note: – MATLAB evaluates expressions to the right of the
equals sign first
– After, it assigns this result to the variable to the left of
the equals sign – Here‟s an example:
sum = 2;sum = sum + 3;
– What happens here? sum gets assigned the value of2 first, then it gets added with 3, and stored back intosum
• … and that‟s it for this section… whew!
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 48/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
48
Outline of Tutorial – (1)
• Introduction
• Getting Started
• Variables and Starting Basics
• Vectors and Matrices
• Basic Operations
• Script Files• Function Script Files
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 49/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
49
Basic Operations – (1)
• Let‟s assume the following: – A and B: Are matrices or vectors / arrays of
compatible dimensions
• Assume they can be added, subtracted, multipliedand divided properly
– n is a scalar (single value number)
• Here‟s a table that provides a good
summary of all of the basic operations youcan perform on matrices and vectors /arrays
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 50/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
50
Basic Operations – (2)
• Here, the elements in the matrices or vectors /arrays can be real or complex
• Addition and Subtraction will just add and
subtract two matrices normally• For vectors, each corresponding component
gets added or subtracted
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 51/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
51
Basic Operations – (3)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 52/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
52
Basic Operations – (4)
• Multiplication (this kind) only works for matrices
– * is for matrix multiplication
• Division is a little bit more complicated – We have left division and right division• Left Division == A-1B
• Right Division == AB-1
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 53/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
53
Basic Operations – (5)
• Transposition: Transposes a matrix or vector
– „ operator
• Real valued entries: Normal Transpose
• Complex valued entries: Tranposes and performs complexconjugate
– .‟ operator
• Real and Complex valued entries: Normal Transpose
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 54/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
54
Basic Operations – (6)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 55/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
55
Basic Operations – (7)
• Exponentiation multiplies a matrix with itself n
times
• For calculations with *, /, \, and ^, there is
something called element-by-elementoperations for vectors / arrays and matrices
– You put a dot operator ( . ) before the operation
• Example: .* or ./ or .\ or .^
– The 1st element of A is *, /, \ or ^ with the 1st elementof B, and that result gets stored
– … don‟t get it? Here‟s an example
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 56/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
56
Basic Operations – (8)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 57/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
57
Basic Operations – (9)• Special Case:
– If you wanted to multiply or divide every number in a matrix orvector / array by a constant, you can just simply do the following:
Example:t = 0 : 2 : 20; t = [0 2 4 … 18 20];
g = 2*t; g = [0 4 8 … 36 40]; or g = 2.*t;
h = t/4; h = [0 0.5 1 … 4.5 5]; or h = t./4;
• All you have to do is multiply or divide by the desired
number – No need to create another matrix or vector / array and do point-by-
point multiplication or division!
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 58/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
58
Basic Operations – (10)
• Special Case #2: – If you wanted to add or subtract every number in a matrix orvector / array by a constant, you can just simply do the following:
Example:t = 0 : 20; t = [0 2 4 … 18 20];
g = t + 2; g = [2 4 6 … 20 22];
h = t - 4; h = [-2 0 2 … 16 18];
• All you have to do is add or subtract by the desired
number – No need to create another matrix or vector / array and add or
subtract that way!
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 59/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
59
Basic Operations – (10)
• Here are some useful commands for matricesand vectors / arrays:
• Assume that V is a vector of arbitrary length andM is a matrix of arbitrary size
• len = length(V); – Gives the number of elements the vector V has, and
stores it into len
• [rows cols] = size(M);
– Gives the number of rows and columns and storesthem into rows and cols respectively
– Don‟t worry about the square braces for now, we‟lldeal with them later.
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 60/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
60
Basic Operations – (11)
• sum(V): Returns the sum of all elements
contained in vector V
• sum(M): Returns a row vector, where
each element is the sum of the columns ofmatrix M
• … confused? Don‟t worry, check out this
example
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 61/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
61
Basic Operations – (12)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 62/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
62
Basic Operations – (13)
• prod(V): Returns the product of allelements contained in vector V
– We multiply every single element in the vector
with each other• prod(M): Returns a row vector, where
each element is the product of thecolumns of matrix M
• … confused? Don‟t worry, check out thisexample
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 63/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
63
Basic Operations – (14)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 64/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
64
Basic Operations – (15)
• Quick quiz:
– Suppose I perform this operation:
•V = n : -1 : 1;
– n can be any number > 0•sum = prod(V);
– What is the above operation equivalently
called in mathematics?
– …you‟ve seen this before. Think MTH 314
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 65/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
65
Basic Operations – (16)
• It’s a factorial!
• Anyway, let‟s keep going
• max(V) / min(V): Returns the maximum /
minimum of all elements contained in vector V• max(M) / min(M): Returns a row vector,
where each element is the maximum / minimumof the columns of matrix M
• … confused again? No worries, here‟s another
example
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 66/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
66
Basic Operations – (17)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 67/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
67
Outline of Tutorial – (1)
• Introduction
• Getting Started
• Variables and Starting Basics
• Vectors and Matrices
• Basic Operations
• Script Files• Function Script Files
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 68/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
68
Script Files – (1)
• So far, you‟ve seen that MATLAB can accept
and execute commands interactively through the
command prompt
– … what happens if you‟ve got A LOT of commands toexecute?
• Think C or Java: You put all commands or
syntax into a file and execute that file.
• You can do this with MATLAB, and you don‟thave to input all of the commands individually.
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 69/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
69
Script Files – (2)
• This kind of file is called a script file or M-file (Mfor MATLAB!) – You place all commands you want to execute in a file
with extension .m at the end, and you run the script
file – MATLAB will run all of these commands in sequence
for you
• To execute a script file, make sure you set the
working directory to be where the script file islocated (remember I said we‟d get back to thisearlier?)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 70/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
70
Script Files – (3)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 71/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
71
Script Files – (4)
• MATLAB has a M-file editor that you can
use where you can create your scripts
– You can also use any other word processing
editor. Just make sure the extension of thefile is .m
– It‟s got:
• Some nice colour coding features
• Debugging Tools
• Interoperability with the MATLAB command prompt
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 72/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
72
Script Files – (5)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 73/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
73
Script Files – (6)
• Once a script file has been created, type in thename of the file (without the .m) in the command
prompt to execute the script
– Make sure you set the proper working directory!
• When you execute a script file, all of the
variables created in the script file get stored in
the workspace for future use
• Let‟s do an example: – Let‟s make the factorial example into a script file
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 74/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
74
Script Files – (7)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 75/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
75
Script Files – (8)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 76/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
76
Script Files – (9)
• The great thing about script files is thatcommands can be re-executed without
having to enter them all again!
– All you have to do is modify parts of the scriptfile to give you the result you want
• The script file is now set to compute 4!
What if I wanted to do 9!, or 12!, or 5! ? – Just change the n parameter accordingly
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 77/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
77
Outline of Tutorial – (1)
• Introduction
• Getting Started
• Variables and Starting Basics
• Vectors and Matrices
• Basic Operations
• Script Files• Function Script Files
S ( )
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 78/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
78
Function Script Files – (1)
• So, you‟ve seen how script files work – So what are function script files?
• Think Java: These are methods – You send input variables into a function
• Difference: You don’t have to define what the variable types are.
Remember: Dynamically Typed! – The function performs MATLAB commands with these input
variables
– The function returns output variables• Difference: You can return more than one variable!
• You don‟t have to define the variable type either!
• Unlike C or Java, you do not require return statementsto return variables – All you have to do is assign something to the variable, and that‟s
it.
F i S i Fil (2)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 79/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
79
Function Script Files – (2)• Why would you want function script files?
– Instead of changing some parts of a script file, youcan do these changes by providing different variablesto the input of the function
– There may be a case where when you run a script,
you don‟t want variables created in the script to besaved to the workspace
• Function scripts only communicate with theMATLAB workspace with:
– The variables that you pass to it – The variables that get sent to the output after it‟s
finished
F ti S i t Fil (3)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 80/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
80
Function Script Files – (3)
• What does this mean? – Any variables you create within the function
will be discarded after the function hasfinished executing
– This is what we call local scope
• How do we create a function script file?
– Pretty much the same as a normal script file
• .m as the extension to the file
– Remember, with a script file, all you had to dowas enter in the commands in sequence
F ti S i t Fil (4)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 81/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
81
Function Script Files – (4)
• However, there are two differencesbetween a script file, and a function script
file
– The file name of the function has to be thesame as the function itself
– The first line of a function script file must be
the following:function [output1, output2,…] =
function_name(input1, input2, …)
F ti S i t Fil (5)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 82/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
82
Function Script Files – (5)• The function keyword means that this script
file is a function script file• input1, input2, … represents the input
variables going into the function
• output1, output2, … represents the output
variables going into the function• You may have noticed that there are square
braces ( [ ] ) surrounding the output variables inthe function header
– In order to return more than one variable, MATLABputs all of the variables into one vector and returnsthis vector, hence the [ ] … smart eh?
F ti S i t Fil (6)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 83/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
83
Function Script Files – (6)
• Use the % operator to put in comments – If you want to perform the modulus, use the mod function
– mod(x,y) == x % y in C or Java
• If you want to comment out a block of code, do thefollowing:
%{… commands that are commented out…
%}
• When you‟re coding a function script file, it‟s a good ideato put an author’s block at the beginning of the file, thattells someone how the function works, what inputs youneed, what outputs come out, and how to use it
F ti S i t Fil (7)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 84/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
84
Function Script Files – (7)• Some examples of function script files that you have
seen already:– eye(n)
• Input: number n, n > 0
• Output: n x n identity matrix
– prod(V), prod(M)• Input: Vector V or Matrix M
• Output: A number or a vector – sum(V), sum(M)
• Input: Vector V or Matrix M
• Output: A number or a vector
– max(V), max(M)• Input: Vector V or Matrix M
• Output: A number or a vector
– min(V), min(M)• Input: Vector V or Matrix M
• Output: A number or a vector
F ti S i t Fil (8)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 85/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
85
Function Script Files – (8)
• Here‟s another one that you saw earlier: – [rows cols] = size(M)
• Input: Matrix M
• Output: 2 numbers, the rows and columns of the matrix
– When you call this function script file in MATLAB,
make sure the square braces are there
– This is required if the function script file returns more
than 1 variable
– Failing to do this will result in the very last output
variable to be saved instead
F ti S i t Fil (9)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 86/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
86
Function Script Files – (9)
• So now with this, let‟s go back to ourfactorial example and make this a function
script file
• What do we need to do in order to makethis a function script file?
– Think back to the two differences that I said
earlier about script files and function scriptfiles
F ti S i t Fil (10)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 87/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
87
Function Script Files – (10)
F ti S i t Fil (11)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 88/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
88
Function Script Files – (11)
F ti S i t Fil (12)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 89/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
89
Function Script Files – (12)
• Some important things to be reminded of:– n in the function script file is not saved to the
workspace•n has a local scope!
– Instead of changing the parameter n, as wedid in a script file, we simply changed theinput parameter of the function
– If your function returns more than onevariable, make sure you save them to avector!
O tli f T t i l (1)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 90/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
90
Outline of Tutorial – (1)
• Introduction
• Getting Started
• Variables and Starting Basics
• Vectors and Matrices
• Basic Operations
• Script Files• Function Script Files
O tline of T torial (2)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 91/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
91
Outline of Tutorial – (2)
• Flow Control
• Basic Graphics Commands
• Other Useful Commands
• Final Words
Flow Control (1)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 92/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
92
Flow Control – (1)
• You‟ve seen this in C or Java many times • Flow control allows your script or function script
file to have decision making abilities
• …understand what I mean? If you don‟t,
MATLAB supports the following common flowcontrol methods– for loop
– while loop
– if-else & else-if statements
– switch-case-otherwise statements
• … now you see what I mean right?
Flow Control (2)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 93/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
93
Flow Control – (2)
• for loops allow a group of commands to beexecuted a predetermined amount of times
• This loop has the following structure:
for i = any_array…
commands to be executed
…
end
• any_array is any valid array
• Can be any row or column vector
Flow Control (3)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 94/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
94
Flow Control – (3)
• How this for loop works is as follows: – 1st iteration, i = any_array(1), then execute
commands
– 2nd iteration, i = any_array(2), then executecommands
– … – nth iteration, i = any_array(n), then execute
commands
• This can prove to be a very powerful mechanism
– Your for loop indices never have to be in uniformsteps!
• Don‟t know what I mean?... Let‟s do an example
Flow Control (4)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 95/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
95
Flow Control – (4)
• First Example: for loop in uniform steps– f = 1;for i = 1:n
f = f*i;
end – Here, we‟re computing our factorial function, but with
the use of a for loop
• i is assigned 1 for the 1st iteration, 2 for the 2nd iteration, and
n for the nth iteration
• For each iteration, we take the previous value off and multiply by the current value of i
Flow Control (5)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 96/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
96
Flow Control – (5)
• Second Example: for loop in non-uniform steps– val = 0;array = 1 : 20;for i = [1 4 8 9 10 2 5]
val = val + array(i);
end – Here, we‟re computing the sum of random elements in an arrayand storing them into val
• Take a close look at what I did in the for loop
• The 1st iteration, i = 1, the 2nd iteration, i = 4, the 3rd
iteration, i = 8, and so on• Pretty cool eh!? You don‟t have to have uniform steps in
your for loops!
Flow Control (6)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 97/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
97
Flow Control – (6)
• while loops let you execute a group ofcommands indefinitely until a condition is
met
• The structure for this loop is as follows:while expression
…
commands to be executed…
end
Flow Control (7)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 98/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
98
Flow Control – (7)• The commands between the while and
the end statements are executed untilexpression is evaluated as false, or azero (0) value
• You usually use relational or equalityoperators in the expression statementof the while loop
• A relational or equality expression isassigned a value of 1 if it‟s true, and 0 ifit‟s false
Flow Control (8)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 99/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
99
Flow Control – (8)
• Everything here is the same as C and Java,
except for the not equals to operator. MATLABhas it as ~=, and C or Java has it as !=… becareful not to mix these up!
Flow Control (9)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 100/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
100
Flow Control – (9)• if-else & else-if statements help you execute
blocks of commands when some condition is true• Here‟s the syntax for it… but I‟m sure you already know
thisif exp_1
… commands to be executed if exp_1 is true
… elseif exp_2…
commands to be executed if exp_2 is true…
elseif exp_3…
commands to be executed if exp_3 is true
… else…
commands to be executed if none of the above is true…
end
Flow Control (10)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 101/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
101
Flow Control – (10)
• Note the following:– elseif and else operators are optional
– You can have if statements by themselves as wellwithout the above operators
• The break command: – Used often in for and while loops
– When this command is encountered, the loop stopsexecuting immediately and carries onto the next block
of code – If you‟re using this within nested loops, the inner most
loop stops executing only
Flow Control (11)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 102/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
102
Flow Control – (11)
• switch-case-otherwise statements are aspecial case (no pun intended) of the if-else & else-if statements
• You choose between a finite number of choices;
each choice consists of a block of commands tobe executed
• If the choice you make is none of the choicesprovided in this statement, we execute a default(otherwise) code block
• … so this is what this kind of statement lookslike:
Flow Control (12)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 103/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
103
Flow Control – (12)
switch exprcase value1,… commands to be executed if expr equals value1…
case value2,… commands to be executed if expr equals value2…
… otherwise,
… commands to be executed if the value of expr isnot equal to any of the above values…
end• You usually use switch statements when you want your program to run
differently, based on the input parameters that are given to your functionscript file
Flow Control (13)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 104/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
104
Flow Control – (13)• Let‟s do an example!
– Let‟s use the while and if-else statements tomake a simple program
– You all have studied logic gates in COE 328, so let‟suse some simple facts from this
– Let‟s make a program that will classify a set of input
voltages according to the following specs:• If Vout is between 0V and 0.5V, this is a logic 0 (0)
• If Vout is between 2.7V and 5V, this is a logic 1 (1)
• All other voltages are classified as invalid (-1)
– The program will read in an array of continuous input
voltages, and output an array of the same size – Each element will take 1 of 3 possible values: 0, 1
and -1
Flow Control (14)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 105/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
105
Flow Control – (14)• What do I need to do?
– Set up a function script file with the function
keyword, and the appropriate inputs and outputs• Let‟s call this function: classify_TTL
• Inputs: V, an array of continuous input voltages
• Outputs: logic_levels, an classification array telling youwhether you have logic 0, logic 1, or invalid voltages
– Naturally, you want to use a for or while loop for
this program because you want to check through
every element in this input array – You use an if-else statement to check to see what
the voltage inputs are, then you classify accordingly
Flow Control – (15)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 106/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
106
Flow Control – (15)
Flow Control – (16)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 107/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
107
Flow Control – (17)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 108/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
108
Flow Control – (17)
• Above, I declared an array of 7 input voltages
and ran the function• The 2nd element is valid as logic 0 because it‟s
between 0V and 0.5V
• The 5th and 6th element is valid as logic 1because they‟re between 2.7V and 5V
• The rest are invalid
Outline of Tutorial – (2)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 109/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
109
Outline of Tutorial – (2)
• Flow Control
• Basic Graphics Commands
• Other Useful Commands
• Final Words
Basic Graphics Commands – (1)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 110/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
110
Basic Graphics Commands (1)
• MATLAB provides a variety of sophisticatedtechniques for presenting and visualizing data
• Also, MATLAB makes it very easy to plot data!
– I have never used MS Excel ever again becauseMATLAB makes it easy!
• The watered down version:
– Provide an array of values for each set of axes
– Run a function that plots things for you – Run a few more commands that will make a grid, set
the title of the graph, the title of the axes and so on
Basic Graphics Commands – (2)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 111/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
111
Basic Graphics Commands (2)
• 2D plotting: MATLAB makes this veryeasy!
– If x and y are arrays of elements that are the
same size, you can plot them using this datawith the following command:plot(x, y);
– This will bring up a window plotting a graph of
y vs. x
– To plot something simple, that‟s just about it!
Basic Graphics Commands – (3)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 112/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
112
Basic Graphics Commands (3)
• Here‟s a basic example if you don‟t believe me • Let‟s say I wanted to plot the line y = x
• Let‟s choose 101 points between 0 to 10 in stepsof 0.1
• Here‟s the syntax I‟d use:
… and that‟s it! • This is what the graph looks like…
Basic Graphics Commands – (4)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 113/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
113
Basic Graphics Commands (4)
Basic Graphics Commands – (5)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 114/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
114
Basic Graphics Commands (5)
• If you want to plot multiple plots on a singlegraph, you do the following:plot(x1,y1,x2,y2,…,xN,yN);
• N is the number of plots you want to appear on
the single graph
• xi and yi are the points to the ith graph you
want plotted on the single graph
• The number of elements between (x1,y1),(x2,y2), … (xN,yN) must all be the same!
Basic Graphics Commands – (6)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 115/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
115
Basic Graphics Commands (6)
• Let‟s do another example: – Let‟s plot the following 5 lines:
y1 = 0.1x
y2 = 0.5x
y3 = 2xy4 = 5x
y5 = 10x
– For now, let‟s make them all go from 0 to 10 in step
sizes of 0.1 – These plots don‟t all have to have the same step size!
Basic Graphics Commands – (7)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 116/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
116
Basic Graphics Commands (7)
• This is what I‟d put into the commandprompt in MATLAB
– You can also make a script file too if you
want!
• … and this is what the graph looks like!
Basic Graphics Commands – (8)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 117/142
ELE 532 – Signals and Systems
MATLAB TutorialFriday, September 14th, 2007
117
Basic Graphics Commands (8)
Basic Graphics Commands – (9)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 118/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
118
Basic Graphics Commands (9)
• MATLAB automatically colour codes thedifferent plots that are on the graph
• This graph looks pretty plain… there‟sgotta be more that you can add, and yes
you can• You can add a title, label the axes, put a
grid on and even a legend!
– You can add these in the graph GUI thatyou‟ve just seen, or do it through thecommand prompt
Basic Graphics Commands – (10)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 119/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
119
Basic Graphics Commands (10)
• So let‟s say I wanted to add a title, add agrid, label the axes and put up a legend,
how would I do that?
Basic Graphics Commands – (11)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 120/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
120
Basic Graphics Commands (11)
Basic Graphics Commands – (12)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 121/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
121
Basic Graphics Commands (12)
• grid puts a grid on the graph – The spacing for the grid is automatically figured out
by MATLAB
• title(‘…’) lets your graph have a title
• xlabel(‘…’), ylabel(‘…’) labels the x andy axes accordingly – Put the labels inside the quotations
– Don‟t forget the quotations „ „!
• legend(‘…’, ‘…’, …, ‘…’) produces alegend, labeling what each plot is on the graph
Basic Graphics Commands – (13)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 122/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
122
Basic Graphics Commands (13)
• Remember, for the legend command,you label the plots the same order as howyou plotted them!
• By default, MATLAB takes all points andconnects them with a solid line, and it‟s gotits own way of determining which colourbelongs to what plot
• Is there a way to control this?... Yes thereis!
Basic Graphics Commands – (14)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 123/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
123
p ( )
• In addition to the x and y points, youspecify an additional parameter:plot(x, y, ‘line_style’);
• line_style is a character string of 2characters
– The 1st character specifies the colour of your
plot – The 2nd character specifies how your plot will
be plotted on the graph, or the plot style
Basic Graphics Commands – (15)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 124/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
124
p ( )
• Supported colours: – blue, green, r ed, cyan, magenta, yellow,
black
• Supported plot styles:
Basic Graphics Commands – (16)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 125/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
125
p ( )
• Examples: x = 0:0.1:10;
y = x;
– plot(x,y,’g.’); • This will plot a green line with dots at each point
– plot(x,y,’bo’);
• This will plot a blue line with circles at each point
– plot(x,y,'rx’);
• This will plot a red line with crosses at each point
Basic Graphics Commands – (17)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 126/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
126
p ( )
• Like how we saw before, you can put multiple plots on asingle graph, each with their own colour and plot styleswith the following:plot(x1, y1, ’line_style1’, x2, y2,’line_style2’,..., xN, yN, ’line_styleN’);
• N is the number of plots you want to appear on the singlegraph
• xi and yi are the points to the ith graph you want plottedon the single graph
• line_stylei is the plot style and colour of that ith
graph • The number of elements between (x1,y1), (x2,y2),… (xN,yN) must all be the same!
Basic Graphics Commands – (18)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 127/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
127
p ( )
• There is also a way to produce multiplegraphs in one window
– You can have more than 1 graph in onewindow at a time!
• You do this by using the subplot command
• The subplot command treats the window
as if it had multiple slots – Each slot takes in a graph
Basic Graphics Commands – (19)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 128/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
128
p ( )
• How do we use the subplot command?– subplot(m,n,p) or subplot(mnp)
– m and n you need to know before hand
• These determine the number of rows (m) and columns (n)
for the amount of graphs you want• p determines which location in the window you want the plot
to go to
• The order is from left to right, top to bottom
– In order to properly use subplot, you must call thisfunction first
– After, you code the syntax to plot something normally
Basic Graphics Commands – (20)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 129/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
129
p ( )• Here‟s a small example:
– If I wanted to make a window that has 4 plots, 2 plots in eachrow 2 rows, here‟s what I‟d do
• Do subplot(221) Specify that we want to work on the top leftcorner
• Next, code the syntax to plot normally. The plot will appear on thetop left corner
• Do subplot(222) Specify that we want to work on the top right
corner• Next, code the syntax to plot normally. The plot will appear on the
top right corner
• Do subplot(223) Specify that we want to work on the bottomleft corner
• Next, code the syntax to plot normally. The plot will appear on thebottom left corner
• Do subplot(224) Specify that we want to work on the bottomright corner
• Next, code the syntax to plot normally. The plot will appear on thebottom right corner
Basic Graphics Commands – (21)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 130/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
130
p ( )
• Let‟s do another example… in case you didn‟tget what I meant earlier
• Let‟s make 4 graphs, have 2 rows and 2columns for the window.
• Each graph will have one plot. – Let‟s make each plot the following: 1. y1 = sin(x);2. y2 = cos(x);3. y3 = 3x;4. y4 = 6x;
– Let‟s make the range of the plot go from:x = -10 : 0.1 : 10;… now what?
Basic Graphics Commands – (22)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 131/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
131
p ( )
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 132/142
Basic Graphics Commands – (24)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 133/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
133
p ( )
Basic Graphics Commands – (25)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 134/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
134
( )
• Some functions, like cos and sin take in, notonly single values, but vectors / arrays and
matrices
• cos and sin apply their respective
mathematical operations to every element if the
input is a vector / array, or matrix
• The output will be a vector / array, or matrix, of
the same size, with the function applied to eachelement in that memory chunk
Outline of Tutorial – (2)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 135/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
135
( )
• Flow Control• Basic Graphics Commands
• Other Useful Commands
• Final Words
Other Useful Commands – (1)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 136/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
136
( )
• MATLAB has a great help facility, boththrough its interface and online
• If you need help regarding how a certain
command works, type in the following inthe command prompt:
– help command
• command is the command you want tolook up and to see how it works
Other Useful Commands – (2)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 137/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
137
( )
• If you don‟t know what a particular functionis called, you can use the lookfor command
• It‟s called the following way: – lookfor command
• Where command is the function you‟relooking for
• MATLAB searches all of its libraries thatare related to command
Other Useful Commands – (3)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 138/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
138
( )
• A list of all of the MATLAB commands you have invokedin a session in the command prompt can be written intoa file with the diary command
• You call this command this way in the command promptdiary diary_file
• diary_file is the name of the file where yourcommands you invoked in that session will be stored
• If the file already exists, your commands in the currentsession will be appended to the file
• Use diary off to turn off the diary command, anddiary on to reactivate it
Outline of Tutorial – (2)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 139/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
139
• Flow Control• Basic Graphics Commands
• Other Useful Commands
• Final Words
Final Words – (1)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 140/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
140
• This tutorial provided you the basics of how touse MATLAB and enough to get you started withthese labs
• This tutorial, however, is not exhaustive
– There are still a lot of commands out there thatperform really cool stuff for you
– Consider taking some time to look at them and seehow cool MATLAB is
• Always use the help and lookfor commandswhen you‟re learning a new function
• If all else fails, ask me!
Final Words – (2)
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 141/142
ELE 532 – Signals and Systems
MATLAB Tutorial
Friday, September 14th, 2007
141
• I adapted this tutorial from Dr. Y. C. Chen‟s – “Introduction to MATLAB” tutorial and can
be found here:
– http://www.ee.ryerson.ca/~rphan/matlabtutorial.pdf • I also wrote a tutorial for the Ryerson IEEE
Student Branch website 3 years ago, and
can be found here: – http://www.ee.ryerson.ca/~ieee/articles/MATLAB.html
Thanks
8/13/2019 Ele532 Matlabtutorial Full
http://slidepdf.com/reader/full/ele532-matlabtutorial-full 142/142
Thanks
ForListening!
Any
Questions?