Introduction To Matlab Exp 1 Part 2
Introduction To Matlab Exp 1 Part 2
Experiment-1
Introduction to MATLAB
Prepared By
Mohammed Abdul Kader
Assistant Professor, Dept. of EEE, IIUC
Objectives:
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.
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
Experiment-1, Digital Signal Processing Sessional, Prepared By- Mohammed Abdul Kader, Assistant Prof, Dept. of EEE, IIUC
If-Else Conditions
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;
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.
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