You are on page 1of 28

Getting Started

with MATLAB
By
Yap Shook Chin
Working with the Desktop
„ Modifying the desktop
layout
„ Sub-Windows:
„ Left upper: Workspace,
Launch Pad, and
Current directory
„ Left lower: Command
History
„ Right: Command
Window
Modifying the Desktop Layout
„ Changing the number and sizes of the sub-
windows
„ Docking/undocking the displayed sub-
windows
„ Setting environment preference
„ Font, color, numeric format, etc.
„ Search Path – directories that MATLAB
searches through
„ Current Directory field
Getting Help
„ Help Æ Matlab Help
„ Launch Pad Æ Help
„ Command window: >> help …
>> lookfor …
„ MATLAB Demos
Workspace and Array Editor
„ Workspace consists of variables currently
held in memory (name, nature, size)
„ Variables can be saved and open in the
.mat file format using command save and
load respectively.
„ Double click a variable to view its content in
array editor
Special Values
„ There are some important variables and
constants used in MATLAB programming.
Matlab as a Simple Calculator
>> (-1+2+3)*5 -2/3
>> 2^3
>> cos (pi/2)

>> p = 2+3;
>> q = 3+5;
>> ratio = p/q
Ratio = 0.6250

>> x = 2+ 3*i
>> abs(x)
>> angle(x)
Arithmetic
Operators
>> A = [2 3 4]
>> B = [1 -2 -3]
>> A.*B
ans = [2 -6 -12]

>> A*B’
ans = -16

>> A’ *B
Ans = 2 -4 -6
3 -6 -9
4 -8 -12
Relational Operators
>> A = [1 2 3 4 5 6]
>> B = [0 2 3 6 5 7]
>> A == B
ans = [0 1 1 0 1 0]

>> A >= B
ans = [1 1 1 0 1 0]
Logical Operators

>> A = [1 2 0]
>> B = [1 -2 3]
>> A & B
ans = [1 1 0]

>> xor(A,B)
Ans = [1 0 0]
Working with Matrix/Array
„ Vector indexing
„ Syntax : >> array_name(row, col)
>> a = [2 4 9 4 6];
>> a(3)
ans = 9

>> a(2:4)
ans = 4 9 4

>>a(3:end)
Ans = 9 4 6

>> a(:) % produce a column vector


>> a(1:end) % produce a row vector
Working with Matrix/Array
„ Matrix indexing
>> A = [1 2 3; 4 5 6; 7 8 9]
A=
1 2 3
4 5 6
7 8 9
>> A(1,3)
ans = 3

>> B = A( : ,3)
B=
3
6
9

>> C = A(2, :)
C=
4 5 6
Working with Matrix/Array
„ Matrix indexing
>> A = [1 2 3; 4 5 6; 7 8 9]

>> D = A(1:2, 1:3)


D=
1 2 3
4 5 6

>> E = A;
>> E( : , 3) =0
E=
1 2 0
4 5 0
7 8 0

>> N = [1,2,3];
>> M = [N, 6:8]
Ans = [1,2,3,6,7,8]
Some Important Standard Array
„ zeros(M,N) generates an M x N of 0s of class double
„ ones(M,N) generates an M x N of 1s of class double
„ true(M,N) generates an M x N logical matrix of 1s.
„ false(M,N) generates an M x N logical matrix of 0s.
„ Rand(M,N) generates an M x N matrix whose entries are
uniformly distributed random numbers in the interval [0,1]

>> S = 7* ones(2,3)
S=
7 7 7
7 7 7
Plotting
„ Linear Plotting
>> x = 0:0.1:2*pi;
>> y = sin(x);
>> plot(x,y)
>> plot(y)
>> plot(x,y,’y’)
>> plot(x,y,’g*’)
>> title(‘Sinosoidal Graph’)
Plotting
„ Adding a grid
>> grid on

„ Adding additional plots to


a figure
>> hold on
>> plot(x,y+1)

„ Deleting/copying the
current object

„ Graph annotation
Programming in Matlab
„ Flow control
„ Functions
„ Variables and workspace
„ Subfunctions
„ Debugging
Flow Control
„ Logic control:
„ If / elseif / else
„ Switch / case / otherwise
„ Iterative loops:
„ for
„ while
Functions
„ Three type of Functions:
„ Core MATLAB (built-in) functions, e.g. sin, abs,…
„ MATLAB-supplied m-file functions, e.g. mead, std,…
„ User-created m-file functions
„ Two types: script and function

„ Functions must be in the MATLAB path so that it


can be recognized

„ Call function
>> output_value = AverageArray([1,2,3])
Function M-file
„ The function definition line
function [av] = AverageArray (x)
„ The H1 line % this function calculate the average
of the output from an equation
% where x = input as an 1D vector
„ Help text % y = output as an 1D vector
% n = size of vector x
% av = average of the output y

n = size(x);
„ The function body
y = 5*x + 8;
av = sum(y)/n(2);
„ comments
Variables and Workspace
„ Variables are stored in workspaces
„ MATLAB workspace
„ For command line and script file variables
„ Function workspace
„ Each function has its own workspace for local
variables.
„ Access via inputs and outputs.
„ Global workspace
„ Global variables can be shared by multiple
workspace
„ Must be initialized in all relevant workspace
Subfunctions
„ Subfunctions can only be called from within
the same m-file
„ Each subfunction has its own workspace
„ Allows more than one function to be within
the same m-file (modularize code)
„ M-files containing subfunctions must have
the name of the first (primary) function.
MATLAB Calling Priority
High
Variable
built-in function
subfunction
private function
Mex-file
P-file
M-file

low

„ Function is called first within same file, then from current


directory, then follow the sequence of MATLAB path.
Interactive I/O
„ To display information on the screen
>> A = [1,2;3,4];
>> disp(A)

>> text = ‘Hello’


>> disp(text)
„ For inputting data
>> x = input(‘Enter x value: ‘)
Enter x value: 3
X=3

>>R = input(‘Enter your name: ‘, ‘s’)


Enter your name: Alex
R = Alex
Vectorizing loop
„ Converting for and while loops to equivalent vector or
matrix operations.
For i = 0:200
x(i+1) = i/100;
y(i+1) = sin(x(i+1)+pi);
end

„ becomes

>> x = 0:0.01:2;
>> y = sin(x*pi);
Preallocating Arrays
„ Preallocate the size of the arrays used in a
program to improve code execution time and
reduce memory fragmentation when working with
large arrays.

„ Allocating memory for an 2 x 3 arrays


>> a = zeros(2,3);

„ Allocating memory for a matrix of an image of size


1024 x 1024
>> f = zeros(1024);
Debuging
„ A breakpoint is set at
the m-file then
debugging can be
done by
„ Step In
„ Step Out
„ Value of variable can
be viewed by pointing
cursor to variable or
invoking array editor.
„ After error has been
rectified, Exit Debug
Mode and Clear All
Breakpoint.
Overview of Practical Session of
UCEC3054 Computer Vision and Imaging

„ Lab 1 – Intro to MATLAB, fundamental of digital


image
„ Lab 2 – Image preprocessing (report 1, 25%)
„ Lab 3 – Image restoration (report 2, 25%)
„ Lab 4 – Image segmentation
„ Lab 5 – mini project part I
„ Lab 6 – image recognition and interpretation
„ Lab 7 – mini project part II
* one report is to be submitted to conclude mini
project part I % II, a demo is required (50%).

You might also like