Sie sind auf Seite 1von 18

27-Jun-17

Introduction to MATLAB Introduction


MATLAB is a software package for high-performance
numerical computation and visualization.

MATLAB stands for MATrix LABoratory and the basic


building block of MATLAB is the array.

Fundamental data-type is the array.


Dr. K. Adalarasu
Vectors, scalars, real matrices and complex matrices are all
Associate Professor automatically handled as special cases of the basic data-
SEEE/EIE type

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 (>>)

Several commands can be typed in the same line. This is done by


typing a comma between the commands. When the Enter key is
pressed the commands are executed in order from left to right

It is not possible to go back to a previous line in the Command


window, make a correction and then re-execute the command

If a command is too long to fit in one line, it can be continued to the


next line by typing three periods (called an ellipsis) and pressing
the Enter key. The continuation of the command is then typed in the
new line.
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017

2
27-Jun-17

9 10
Working in the Command Window

The semicolon (;):


If a semicolon is typed at the end of a command the
ellipsis
output of the command is not displayed

If several commands are typed in the same line, the


output from any of the commands will not be displayed
if a semicolon is typed between the commands instead
of a coma

This is useful when the result is obvious or known or


when the output is very large

Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017

11 12

Working in the Command Window Defining Variables


The percent (%): What is a variable?
A variable is a name made of a letter or a combination of
The compiler does not execute anything that several letters (and digits) that is assigned a numerical
follows the symbol % value.
A variable is actually a name of a memory location.
The text that follows the percent is treated as a
When the variable is used its stored data is used.
comment
If the variable is assigned a new value the content of the
memory location is replaced.

Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017

3
27-Jun-17

13 14

Defining Scalar Variables Rules about Variable Names

Assignment Operator: In MATLAB the = sign is called the Variable names:


assignment operator. It assigns a value to a variable. Can be up to 63 characters long

Can contain letters, digits and underscore character


Syntax:
Must begin with a letter
Variable_name = A numerical value, or a computable expression
Is case sensitive; it distinguishes between uppercase and lowercase

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

pi The irrational number

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

General Commands General Commands

Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017

5
27-Jun-17

21 22

General Commands Operator Precedence


Arithmetic:
(1) Power ^, (2) Multiplicative *,/, (3) Additive +,-

Relational:
(4) inequality <,>,<=,>= (5) equality ==, ~=

Logical: (6) &, (7) |

Evaluation: Left to Right


When in doubt always use parenthesis!!

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

format short Fixed point with 4 decimal digits for: >>290/7


Expression Result (default) 0.001<=number <=1000 ans = 41.4286
x = 10/5*3 x=6 Otherwise display format short e
format long Fixed point with 14 decimal digits for: >>290/7
a = 12/4*58 + (4*3/12) a=8 0.001<=number <=1000 ans =
Otherwise display format long e 41.42857142857143
b = exp(pi/2i) b = 0.0000 - 1.0000i
format short e Scientific notation with 4 decimal >>290/7
b = exp(pi/2*i) b = 0.0000 + 1.0000i digits ans = 4.1429e+001
format long e Scientific notation with 14 decimal >>290/7
digits ans =
4.142857142857143e
+001
Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017

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

Creating 2D Arrays - Example


Creating 1D Arrays - Example
Creating a vector with constant spacing by specifying Zeros, ones and eye commands
the first term (m), the spacing (q) and the last term (n):
Example - 1 Example 2 Example 3
>> zr = zeros(3,4)
zr = >> ne = ones(4,3) >> idn = eye(5)
Syntax: ne = idn =
0 0 0 0
variable_name = [m:q:n] or variable_name = m:q:n 0 0 0 0 1 1 1 1 0 0 0 0
0 0 0 0 1 1 1 0 1 0 0 0
Example 1 Example 2 Example 3 Example 4
1 1 1 0 0 1 0 0
>> x = [1:2:13] >> x = 1:5 >> x = 12:-3:1 >> x = 12:1
x=
1 1 1 0 0 0 1 0
x= x= x=
1 3 5 7 9 11 13 12345 12 9 6 3 Empty matrix: 1-by-0 0 0 0 0 1

Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017

7
27-Jun-17

29 30

Creating 2D Arrays - Example


Notes about variables in MATLAB
All variables in MATLAB are double precision arrays

The variable is defined by the input when the variable is


assigned. There is no need to define the size of the array before
the elements are assigned

Once a variable exists, it can be changed to be any other size or


type of variable

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

Elementary Math Functions

Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017

35 36

Matrix functions - Transpose Operator

Example 1 Example 2 Example 3 Array Addressing


>> aa=[3 8 1]; >> C=[1 2 3;4 5 6;7 8 9] >> aa=[1 2 3]; Elements in an array (either 1D or 2D) can be
>> bb = aa' C= >> bb = addressed individually or in subgroups
bb = 1 2 3 transpose(aa)
3 4 5 6 bb =
7 8 9 1 2 3
8
>> D = C' Useful when there is a need to
1
D= Redefine only some of the elements
1 4 7
2 5 8 Use specific elements in calculation
3 6 9 Define a subgroup of the elements to a new variable

Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017

9
27-Jun-17

37 38

2D Array Addressing - Example

1D Array Addressing - Example Example: The address of an element


>> mat = [1 2 3;4 5 6;7 8 9]
mat = in a matrix is its position
Example The address of an 1 2 3 defined by the row number
>> vec = [1 2 3 4 5 6 7 8 9 10]
element in a vector
4 5 6 and column number where it
vec = 7 8 9
1 2 3 4 5 6 7 8 9 10 is its position in the >> mat(2,3) - mat(1,2) is located.
>> vec(4) row (or column). ans =
ans = 4
4 >> mat(3,1) mat(k,p) refers to the
>> vec(2) + vec(8) ans =
element in row k and
ans = The first position is 7
column p
10 >> mat(3,1) = 77
>> vec(6) = 66
1 and vec(k) refers mat =
vec = to the element in 1 2 3
1 2 3 4 5 66 7 8 9 10 position k 4 5 6
77 8 9

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

Built-in Functions for Handling Arrays

Deleting elements Function Description Example


size(A) Returns a row vector [m,n], where >>A = [1 2 3 4 5
>> mtr = [1 2 3 4;5 6 7 8;9 10 11 12] m and n are the size m x n of the 6 7 8 9 10];
mtr = array A
1 2 3 4 >>size(A)
5 6 7 8 ans =
9 10 11 12 2 5

>> mtr(:,2:4) = [] length(A) Returns the number of elements in >>A=[1 2 3 4];


mtr = vector A or returns max(size(A)) >>a = length(A)
1 when A is a matrix a=
5
9 4

Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017

43 44

Mathematical Operations with Arrays


Mathematical operations with arrays
Array Addition and Subtraction
Array Multiplication
>> A = [5 -3 8;9 2 10];
>> B = [10 7 4;-11 15 1];
>> C = A + B >> A = [1 4 2;5 7 3;9 1 6;4 2 8]; B = [6 1;2 5;7 3];
C= >> C = A*B
15 4 12 C= >> E = 3*A
-2 17 11 28 27 E=
65 49 3 12 6
>> D = C 8 98 32 15 21 9
D= 84 38 27 3 18
7 -4 4 12 6 24
-10 9 3 >> D = B*A
??? Error using ==> *
Inner matrix dimensions must agree.

Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017

11
27-Jun-17

45 46

Mathematical operations with arrays


Element by element operations Built in functions for analyzing arrays
>> A = [1 2 3 4 5 6];
Symbol Description
Element by element >> B = [1 2 3 4;5 6 7 8;9 10 11 12];
multiplication, division, and .* Multiplication
exponentiation of two matrices is Function Description Example
accomplished in MATLAB by .^ Exponentiation >>mean(A)
typing a period in front of the If A is a vector, returns the mean
ans =
arithmetic operator .\ Left Division value of the elements of the vector
3.5
mean(A)
./ Right Division If A is a matrix, returns a row vector >> mean(B)
containing the mean value of each ans =
of the column in A 5 6 7 8

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];

Function Description Example Function Description Example


>>C = max(A) >>C = min(A)
If A is a vector, C is the largest The same as max(A), but for the
C= C = min(A) C=
element of A. If A is a matrix, C is smallest element
6 1
C = max(A) a row vector containing the
>>C = max(B)
largest element of each column >>[d, n] = min(B)
C=
of A d=
9 10 11 12
>>[d,n] = max(A) The same as [d, n] = max(A), but 1 2 3 4
If A is a vector, d is the largest [d, n] = min(A)
d= for the smallest element
element in A, n is the position of
[d,n] = max(A) 6 n=
the element (the first if several
n= 1 1 1 1
have the max value)
6

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];

Function Description Example Function Description Example


>>sum(A) >>median(A)
If A is a vector, returns the sum of If A is a vector, returns the median
ans = ans =
the elements of the vector value of the elements of the vector
21 3.5
sum(A) median(A)
If A is a matrix, returns a row vector >> sum(B) If A is a matrix, returns a row vector >> median(B)
containing the sum of each of the ans = containing the median value of each ans =
column in A 15 18 21 24 of the column in A 5 6 7 8

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!

Strings can be assigned to variables by simply typing the string on


the right side of the assignment operator:

>> B = 'My name is John Smith'


B=
My name is John Smith

Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017

55 56

Creating and Saving a Script file


Usually script files are created and edited using the
Run icon
editor/Debugger window Comments

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

Once the window is open, the commands of the script


file are typed line by line. MATLAB automatically
numbers a new line every time the enter key is pressed

Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017

14
27-Jun-17

57 58

Input to Script File


Value of variables can be loaded from a file using
Click here to Click here to
change the browse for a dlmread Read ASCII delimited file
directory folder wk1read Read spreadsheet (WK1) file
xlsread Read spreadsheet (XLS) file
fscanf Read formatted data from file
textread Read formatted data from text file
sscanf Read string under format control
strread Read formatted data from text string
fread Read binary data from file

Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 57 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017

59 60

Output Commands Output Command fprintf


Use the following commands to save to a file >> x = 1:5; y = sqrt(x); T = [x;y];
>> fprintf('If the number is: %i, its square root is:
%f\n', T);
dlmwrite Write delimited text file
If the number is: 1, its square root is: 1.000000
wk1write Write spreadsheet (WK1) file
If the number is: 2, its square root is: 1.414214


fprintf Write formatted data to file
If the number is: 3, its square root is: 1.732051


sprintf Write formatted data to string

If the number is: 4, its square root is: 2.000000
fwrite Write binary data to file If the number is: 5, its square root is: 2.236068

Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017 Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017

15
27-Jun-17

Syntax for function file


61 62
Conditional Statements
A conditional statement is a command that allows
MATLAB to make a decision of whether to execute a
Function definition line
group of commands that follow the conditional
The H1 line
statement, or to skip these commands
Help text
There are 2 implementations this:
Using if statement there are three variations of this
Function body implementation
if end
Assignment of values to output arguments
if else end

if elseif end

Using switch case statement

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

for end Loops Nested Loops and Nested conditional


statements
for k=f:s:t

A group of MATLAB commands

end
for k=1:n
for h=1:m
k Loop index variable A group of
f The value of k in the first pass commands
s The increment in k after each pass end
A group of commands
t The value of k in the last pass
end

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

Biosignal Processing Biosignal Processing


To generate various test signals using MATLAB To generate various test signals using MATLAB
Unit impulse sequence: Unit Ramp sequence:
(n)=1 for n=0 r(n) =n for n0
=0 for n0 =0 for n<0
Unit Step sequence: Exponential Sequence:
u(n) =1 for n0 x(n)=an for all
=0 for n<0

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+)

Dr. KA, MATLAB, Biosignal Processing Lab, June Dec 2017

18

Das könnte Ihnen auch gefallen