Dr. Eng.
/ Ghada El Banby
Minoufia University ,Faculty of Electronic Eng. Dep.
of Ind. Electronics and Control Eng.
1
Matlab
Stands for MATrix LABoratory.
Scientific programming environment.
Very good tool for the manipulation of
matrices.
Loads of built-in functions.
Easy to learn and simple to use.
2
The MATLAB Environment
• MATLAB window
components:
Workspace
➢Displays all the defined
variables
Command Window
➢ To execute commands in
MATLAB environment
Command History
➢Displays record of the
commands used
3
القوائم
الادوات الاساسية مكان حفظ امللفات
تسجيل كل املدخالت واملخرجات هنا
نافذة الاوامر لكتابة الاوامر
امللفات املحفوظة
وأظهار النواتج سوياً
تسجيل كل الاوامر هنا
Variables int a;
Don’t have to declare type double b;
Just assign in command window float c;
>>
>> a=12; % variable a is assigned 12 double
precision
Matlab
prompt suppress comment
assign operator
command
operator
output
Try the same line without
the semicolon and
comments
5
Variables (continued …)
View variable contents by simply typing the
variable name at the command prompt
>> a
a=
12
>>
>> a*2
a=
24
>>
6
Workspace
The workspace is Matlab’s memory
Can manipulate variables stored in the
workspace
>> b=10;
>> c=a+b
c=
22
>>
7
Workspace (continued …)
Display contents of workspace
>> whos
Name Size Bytes Class
a 1x1 8 double array
b 1x1 8 double array
c 1x1 8 double array
Delete variable(s) from workspace
>> clear a b; % delete a and b from workspace
>> whos
>> clear all; % delete all variables from workspace
>> whos
8
Matlab help commands
help
>> help whos % displays documentation for the function whos
>> lookfor convert % displays functions with convert in the first help
line
Start Matlab help documentation
>> helpdesk
9
Matrices
Don’t need to initialise type, or dimensions
>>A = [3 2 1; 5 1 0; 2 1 7]
A=
3 2 1 square brackets to define
5 1 0 matrices
2 1 7 semicolon for next row in
matrix
>>
10
Manipulating Matrices A=
3 2 1
Access elements of a matrix 5 1 0
>>A(1,2) 2 1 7
ans=
2 indices of matrix
element(s)
11
The operator :
VERY important operator in Matlab
Means ‘to’
>> 1:10
ans =
1 2 3 4 5 6 7 8 9 10
>> 1:2:10
ans =
1 3 5 7 9
12
The operator : and matrices A=
>>A(3,2:3) 3 2 1
ans = 5 1 0
2 1 7
1 7
>>A(:,2)
ans =
2
1
What’ll happen if you type A(:,:) ?
1
13
A=
3 2 1
5 1 0
Manipulating Matrices 2 1 7
>> A ' % transpose B=
>> B*A % matrix multiplication 1
4
3
9
1
5
>> B.*A % element by element multiplication2 7 2
>> B/A % matrix division
>> B./A % element by element division
>> [B A] % Join matrices (horizontally)
>> [B; A] % Join matrices (vertically)
Create matrices A and B and try out the matrix operators in this slide
14
Operators
== Equal to
(relational, logical)
~= Not equal to
< Strictly smaller
> Strictly greater
<= Smaller than or equal to
>= Greater than equal to
& And operator
| Or operator
Operators (arithmetic)
+ addition
- subtraction
* multiplication
/ division
^ power
‘ transpose
MATLAB m-files
Two types of m-files
script files
collection of commands that Matlab executes
when the script is “run”
function files
collection of commands which together
represent a function, a procedure or a method
Both types are separate files with a “.m” extension
MATLAB m-files
To create an m-file, open the Matlab text editor
Click on the “page” icon
The Matlab text editor window will open
MATLAB m-files
Script Files
On the command line In the script file named test.m
>>x=3.0;
>>y=x^2;
>>y
y =
9.0
>>
On the command line
>>test
y =
9.0
>>
MATLAB m-files
Function Files
Matlab identifies function files from script files by
using the “function”
the name of the function file must be
the same name as the function
MATLAB m-files
Function Files
The function file x2.m
>>r=3; >>h=x2(4.2);
>>d=x2(r); >>h
>>d h =
d = 17.64
9.0 >>
>>
Flow
if
Control
for
while
break
….
23
24
25
More flow control
While statement block
Without ; to
print output
i=
4
i=
16
i=
256
26
Notes:
“%” is the neglect sign for Matlab (equaivalent of “//” in
C). Anything after it on the same line is neglected by
Matlab compiler.
Sometimes slowing down the execution is done
deliberately for observation purposes. You can use the
command “pause” for this purpose
pause %wait until any key
pause(3) %wait 3 seconds
28
29
30
Subplots
• subplot(m,n,p) : create a subplot in an array of axes
>> subplot(2,3,1);
>> subplot(2,3,4);
m
n
32
33
Read an Image
I = imread(‘[Link]’);
Display an Image
imshow(I)
34
Display size an Image
y= size(I);
display portions of an image
by specifying the range
imshow(I(200:260,150:220))
35
M-file for Loading & Displaying
Images
36
imtool(I) “display images and do simple
image manipulations”
37
38
Writing an Image (imwrite)
In order to save an image you must use the imwrite
function in MATLAB
39
40
Convert an RGB image to a
grayscale image.
41
M-file for Creating Binary Images
(im2bw)
42
RGB Grayscale Black & White Morphological
operations
43
44
Using the binary image , calculate region properties of
objects in the image such as area, diameter, etc…
An object in a binary image is a set of white pixels (ones)
that are connected to each other.
using the bwlabel command:
[L, num] = bwlabel(I)
L gives the labeled image, and num gives the number of
objects
45
46
regionprops command
D = regionprops(L, 'area', 'perimeter');
47
48
49
Information about graphics file
(imfinfo)
50
Add two images or add constant to
image (imadd)
51
52
53
54