International Islamic University Chittagong
Dept. of Electrical and Electronic Engineering
Experiment-1
Introduction to MATLAB
EEE-3604 Digital Signal Processing Sessional
Prepared By
Mohammed Abdul Kader
Assistant Professor, Dept. of EEE, IIUC
Objectives:
a) To familiarize with MATLAB and some basic commands of MATLAB.
b) To know about variable and variable types in MATLAB.
c) To know about Matrix manipulation in MATLAB.
d) To learn about plotting 2D graphs in MATLAB.
e) To become familiar with MATLAB script/editor.
f) To become familiar with conditional operators and loops in MATLB.
g) To learn about debugging program in MATLAB.
h) To know how to develop an user defined function in MATLAB
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Scripts
Matlab editor: easy to save, edit, run and debug program.
Used to develop function.
Use scripts to execute a series of Matlab commands.
Press to create new
m-file in the matlab
editor
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Scripts (Cont.)
Scripts will manipulate and store variables and
matrices in the Matlab Workspace (memory).
They can be called from the Matlab command
line by typing the (case sensitive!) filename of
the script file.
>> myscript
Scripts can be opened in the editor by the
following
>> open myscript
Highlight a few lines of your script by left- clicking and
dragging the mouse over the lines. Right-click the highlighted
lines and select Evaluate Selection.
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Live Scripts
Instead of writing code and comments in plain
text, you can use formatting options in live
scripts to enhance your code.
Live scripts allow you to view and interact with
both code and output and can include formatted
text, equations, and images.
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
If-Else Conditions
Syntax-1 Syntax-2 Syntax-3 Syntax-4
if condition if condition if condition if condition
block block 1 block 1 block 1
end else elseif condition elseif condition
block 2 block 2 block 2
end … …
elseif condition elseif condition
block N block N
Relational Operator: end else
== (Determine equality) block
~= (Determine inequity)
Logical Operator:
end
>= (Determine greater than or equal to) && (AND)
<= (Determine less than or equal to) || (OR)
< (Determine less than) ~ (NOT)
> (Determine greater than)
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
If-Else Conditions Example: Finding the type of a MATLAB variable.
Example: Checking whether a number
A=[3,1];
is even or odd.
number=input('Enter Number; '); [r,c]=size(A);
if (rem(number,2)==0) if r==1 && c==1
disp('Number is even'); disp('A is scaler');
else
elseif (r==1 && c>1)
disp('Number is odd');
end disp('Row Vector');
Printing/Displaying Output in MATLAB elseif (r>1 && c==1)
a) leaving the semicolon off after instruction. disp('Column Vector');
b) “disp” function: It can show variable or text. else
c) “fprintf ” function: which accepts a C printf-style disp('A is MATRIX');
formatting string. end
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Switch Statement
Example-1
Syntax: n = input('Enter a number: ');
switch switch_expression
case case_expression switch n
statements case -1
case case_expression disp('negative one')
statements case 0
... disp('zero')
otherwise case 1
statements disp('positive one')
end otherwise
disp('other value')
end
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Switch Statement (Cont.)
Example-2
x = [12 64 24]; Example-3
plottype = 'pie3'; result = 52;
switch plottype switch(result)
case 'bar' case 52
bar(x) disp('result is 52')
title('Bar Graph') case {52, 78}
case {'pie','pie3'} disp('result is 52 or 78')
pie3(x) end
title('Pie Chart')
otherwise
warning('Unexpected plot type. No plot created.')
end
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
For loop
Example: Finding average of numbers
for index = values
Syntax: statements clc % clear command window
end clear all % clear workspace
index=initVal:endVal disp('Find average');
Increment the index variable from initVal to endVal by 1,
and repeat execution of statements until index is greater
n=input('How many numbers: ');
than endVal. sum=0;
for index = 0:10 for i=1:1:n
statements number(i)=input('');
end
sum=sum+number(i);
index=initVal:step:endVal
Increment index by the value step on each iteration, or end
decrements index when step is negative. fprintf('The average is: %f',sum/n);
for v = 1:-0.2:0
statements
end
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
While loop
while expression Example: Construction of Sawtooth wave.
statements 4 −4 4
𝑦 = sin 2𝜋𝑓𝑡 + sin 2𝜋 2𝑓 𝑡 + sin 2𝜋 3𝑓 𝑡 +
end 𝜋 2𝜋 3𝜋
−4
(4𝜋) sin 2𝜋 4𝑓 𝑡 +……..
N.B. while loop to repeat when
expression/condition is true. m=input('Number of harmonics:');
t=0:0.001:2;
y=0;
Example: Finding the factorial of a number. n=1;
while n<=m
n = input('Enter the Number:');
y=y+(4/(n*pi))*sin(2*pi*n*t);
f = n;
n=n+1;
while n > 1
end
n = n-1;
plot(t,y,'g','linewidth',3);
f = f*n;
end
fprintf('The factorial of %d is %d \n', n,f);
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Function
✓ A function is a group of statements that together perform a task.
✓ Functions allow the programmer to break down large, complex problems to smaller and more manageable
pieces.
✓ A function is a reusable set of instructions.
Main Program function
……………………. …………..
..………………….. …………
…………………… ………….
……………………. ………….
Call function
……………………..
……………………
……………………..
…………………….
Call function
………………….
……………………
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Functions in MATLAB
A MATLAB function is a special type of M-file that runs in its own workspace.
Users can write functions which can be called from the command line.
Functions can accept input variable(s)/matrice(s) and will output variable(s)/matrice(s).
Functions will not manipulate variable(s)/matrice(s) in the Matlab Workspace.
Remember that the filename of a function will be its calling function name.
Don’t overload any built-in functions by using the same filename for your functions or scripts!
Functions can be opened for editing using the open command. Many built-in Matlab functions can
also be viewed using this command.
Syntax of User defined function in MATLAB
function [out_arg1, out_arg2, …]= function_name (in_arg1, in_arg2, …)
Function body
end
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Syntax of Functions
Having no input and output Having input arguments and no Having both input and output
arguments output arguments arguments
function name function [out_arg1, …]= ]=name(in_arg1, …)
function name(in_arg1, in_arg2, …)
end end
end
Example Example
Example
function txt(a) function [Y]=reshape_matrix(A,r,c)
function txt switch a if r>0
disp(‘This is a sample program’); case 1 A(r,:)=[];
end disp(‘1st text is showing function’); end
case 2 if c>0
disp(‘2nd text is showing function’); A(:,C)=[];
case 3 end
disp(‘3rd text is showing function’); Y=A;
otherwise end
disp(‘Nothing to show’);
end
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
Sub-Function
Example
function [Y, Aavg, Ravg]=reshape_matrix_info(A,r,c)
Aavg=avg(A);
if r>0
A(r,:)=[];
end
if c>0
A(:,C)=[];
End
Y=A;
Ravg=avg(Y);
End
function mean=avg(M)
[p,q]=size(M);
mean=sum(M(:))/(p*q);
end
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC