Professional Documents
Culture Documents
Room No: VV351 Built-in functions are optimized for vector operations.
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
3 4
MATLAB Windows
Window Purpose
Command window Main window, enters variables, runs programs
Figure window Contains output from graphic commands
Editor window Creates and debugs script and function files
Help window Provides help information
Command History Logs commands entered in the command
window window
Workspace
Provides information about the variables used
window
Current directory
Shows files in the current directory
window
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
1
27-Jun-17
5 6
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
7 8
Working in the Command Window
To type a command the cursor must be placed next to the command
prompt (>>)
2
27-Jun-17
9 10
Working in the Command Window
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
11 12
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
3
27-Jun-17
13 14
Example-2: letters
Example-1:
>>x = 15 >>x = 15; Avoid using the names of built in functions for a variable. Once a
x= >>y = 3*x 12 function name is used to define a variable, the function cannot be
15 y= Example-3: used
>>x = 3*x 12 33 >>y = 3*x 12 Avoid using predefined variables as variable names
x= >>x ??? Undefined function or variable x'.
33 x=
15
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
15 16
Predefined Variables
Variable name Description Example
A variable that has the value of the last expression that
ans
was not assigned to a specific variable
Example-4:
The smallest difference between two numbers. Equals >>x = 15 Example-6:
eps Example-5:
to 2-52, approx. 2.2204e-016 >>x = 0, y = sin(x)/x
x= >>x = 0; y = sin(x)/x
inf Used for infinity 15 x=
y=
Stands for Not-a-Number. Used when MATLAB cannot >>3*x 12 0
NaN NaN
determine a valid numerical value. Eg: 0/0 ans = y=
33 NaN
i Defined as sqrt(-1), which is: 0 + 1.0000i
j Same as i
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
4
27-Jun-17
17 18
General Commands General Commands
not lost
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
19 20
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
5
27-Jun-17
21 22
Relational:
(4) inequality <,>,<=,>= (5) equality ==, ~=
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
23 24
Display Formats
Examples
Command Description Example
6
27-Jun-17
25 26
Creating Arrays Creating 1D Arrays - Example
Array is a fundamental form that MATLAB uses to store
and manipulate data
Row vector: Row vector:
>> rowvec = [1 2 3 4] >> rowvec = [1,2,3,4]
One dimensional array is a list of numbers placed in a row rowvec = rowvec =
1234
or column 1234
Column vector:
Syntax: Column vector: >> colvec = [1
variable_name = [type elements with a space or comma between the >> colvec = [1;2;3;4]
2
colvec =
elements] Row vector 1
3
4]
2
colvec =
variable_name = [type elements with a semicolon between them or 3 1
press the Enter key after each element] Column vector 4 2
3
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 4 Lab, June Dec 2017
Dr. KA, MATLAB, Biosignal Processing
27 28
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
7
27-Jun-17
29 30
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
31 32
Elementary Math Functions Elementary Math Functions
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
8
27-Jun-17
33 34
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
35 36
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
9
27-Jun-17
37 38
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
39 40
Array Addressing Using Colon Example
Deleting Elements
>>A = [1 3 5 7 9 11;2 4 6 8 10 12;3 6 9 12 15 18;4 8 12 16 20 24;5 10 15 20 25 30]
A= An element, or a range of elements, of an existing variable
>> C = A(2,:)
1 3 5 7 9 11
C=
can be deleted by reassigning nothing to these elements.
2 4 6 8 10 12
2 4 6 8 10 12 This is done by using square brackets with nothing typed in
3 6 9 12 15 18
4 8 12 16 20 24 between them.
5 10 15 20 25 30 >> E = A(2:4,:)
E=
2 4 6 8 10 12 >> kt = [1 2 3 4 5 6 7 8 9 10]
>> B = A(:,3)
3 6 9 12 15 18 kt =
B=
4 8 12 16 20 24 1 2 3 4 5 6 7 8 9 10
5
6
9 >> F = A(1:3,2:4) >> kt(6) = []
12 F= kt =
15 3 5 7 1 2 3 4 5 7 8 9 10
4 6 8
6 9 12
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
10
27-Jun-17
41 42
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
43 44
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
11
27-Jun-17
45 46
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
47 48
Built in Functions for Analyzing Arrays Built in Functions for Analyzing Arrays
>> A = [1 2 3 4 5 6]; >> A = [1 2 3 4 5 6];
>> B = [1 2 3 4;5 6 7 8;9 10 11 12]; >> B = [1 2 3 4;5 6 7 8;9 10 11 12];
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
12
27-Jun-17
49 50
Built in Functions for Analyzing Arrays Built in Functions for Analyzing Arrays
>> A = [1 2 3 4 5 6]; >> A = [1 2 3 4 5 6];
>> B = [1 2 3 4;5 6 7 8;9 10 11 12]; >> B = [1 2 3 4;5 6 7 8;9 10 11 12];
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
51 52
Generation of Random Numbers
Built in Functions for Analyzing Arrays
rand command
>> A = [1 2 3 4 5 6];
The rand command generates uniformly distributed numbers with values from 0
>> B = [1 2 3 4;5 6 7 8;9 10 11 12];
and 1. The command can be used to assign these numbers to a scalar, a vector or a
matrix
Function Description Example
If A is a vector, returns the standard >>std(A)
Command Description
deviation of the elements of the ans =
std(A) or vector 1.8708 rand Generates a single random number
std(A,0) If A is a matrix, returns a row vector >> std(B)
rand(1,n) Generates an n elements row vector of random numbers
containing the standard deviation of ans =
each of the column in A 4 4 4 4 rand(n) Generates an n x n matrix with random numbers
normalizes by N instead of N-1, >>std(A,1)
rand(m,n) Generates an m x n matrix with random numbers
std(A,1) where N is the number of elements ans =
in the vector A 1.7078
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
13
27-Jun-17
53 54
Strings and Strings as Variables String Functions
A string is an array of characters. It is created by typing the
characters with single quotes
Strings can include letters, digits, other symbols, spaces
Example: ad ef, 3%fr2, {edcba21!
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
55 56
Program or
The window is opened from the command window by commands
going to the File menu and then New and then selecting
the M-file
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
14
27-Jun-17
57 58
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 57 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
59 60
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
15
27-Jun-17
if elseif end
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
63 64
The if else end structure switch case Statement
MATLAB Program
switch switch expression
MATLAB Program case value1
if conditional expression Group 1 of commands
Group 1 of
case value2
MATLAB command
elseif conditional expression Group 2 of commands
Group 2 of case value3
MATLAB command Group 3 of commands
else
Group 3 of otherwise
MATLAB command Group 4 of commands
end
end
MATLAB Program MATLAB Program
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
16
27-Jun-17
65 66
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
Solution to Exercise
67 68
Exercise
A vector is given by x = [15,-6,0,8,-2,5,4,- x = [15,-6,0,8,-2,5,4,-10,0.5,3];
10,0.5,3]. Using conditional statements and sum = 0;
loops write a program that determines the sum of for a=1:length(x)
positive elements in the vector if x(a)>0
sum = sum + x(a);
end;
end;
fprintf('The sum of positive numbers in x is: %5.2f \n', sum);
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
17
27-Jun-17
69 70
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017
71
Biosignal Processing
Sine Signal:
x(t)=Asin(t+)
Cosine Signal:
x(t)=Acos(t+)
18