You are on page 1of 16

Signals and Systems

(EL-223)

LABORATORY MANUAL
Spring 2020

Dr. Shahzad Saleem

Engr. Fakhar Abbas

INTRODUCTION TO MATLAB II

(LAB # 02)
Student Name: ______________________________________________

Roll No: ________________ Section: ____


Date performed: _____________, 2020

MARKS AWARDED: ________ / 20

NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES, ISLAMABAD

Prepared by: Engr. Fakhar Abbas Version: 3.00

Verified by: Dr. Waqas bin Abbas, Dr. Shahzad Saleem Updated: Jan 19,2020
Introduction to MATLAB LAB 01
____________________________________________________________________________________________________________________________________________________________

Learning Objectives:
In this lab, you will learn:
 Saving Command Window Data
 Colon Operator
 MATLAB variables
 Matrix manipulation

Software Used:
MATLAB
Saving Command Window data
Before going into today’s lab contents, one should know how to save command window data while
working in command window, it is different than commandhistory which shows all the commands
written in command window since the beginning of MATLAB.
Method 1: First of all, change the default directory (which is normally C:\Program
Files\MATLAB\R2015b) and select Folder by clicking on icon where do you want to save
your command window work
Write diary (‘Name of File whatever you want’) or diary Any_Name_Without_Spaces, it will
create desired file in your current folder.
Now write, any number of commands in command window, you may also use clc or clear
command as well. The contents will be written in file only if you enter command diary off. After
this you can’t further save commands in your file unless you write diary on command.
Method 2:
Write diary (‘File_Name.m’) in command window, it will create File_Name.m file in your current
folder. After writing commands, write diary off at the end. To resume it again, write diary
(‘File_Name.m’).
Working Examples: write following in the command window
>> diary SNS_Lab_2
>> s1 = 'We will cover Colon Operator, Variable Types and Plotting today in detail'
s1 =
We will cover Colon Operator, Variable Types and Plotting today in detail
>> a=3;
>> b=1:5
b=
Columns 1 through 4
1 2 3 4
Column 5
5
>> randi(5,1,4)
ans =
1 2 3 5
>> diary off
Now, check the file SNS_Lab_2, you will see the above contents written in the file. To write
contents again, use diary on command and so on.

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 2 of 16


Introduction to MATLAB LAB 01
Linearly Spaced Vectors
These are generated either by colon (:) operator (discussed in previous lab) or by linspace ()
command. For example,
a=0:1/10:1; and b=linspace (0,1,11); will generate same vector.
The general form of linspace is as follows;
linspace (X1, X2) generates a row vector of 100 linearly equally spaced points between X1 and X2.
linspace (X1, X2, N) generates N points between X1 and X2. For N = 1, linspace returns X2.
Consider the following example of plotting covered in Lab 1, We will do it using linspace () now,

If you replace above t as t=linspace (0,8,801); you will get the same output plot.

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 3 of 16


Introduction to MATLAB LAB 01

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 4 of 16


Introduction to MATLAB LAB 01

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 5 of 16


Introduction to MATLAB LAB 01

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 6 of 16


Introduction to MATLAB LAB 01

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 7 of 16


Introduction to MATLAB LAB 01

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 8 of 16


Introduction to MATLAB LAB 01

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 9 of 16


Introduction to MATLAB LAB 01

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 10 of 16


Introduction to MATLAB LAB 01

Matlab Code for Variable Types: -


% example script file to introduce variable types
% variable containing numerical values
clear;clc;close
A=[1,2,3;4,5,6]
B=[-2,4;0,1;7,8;-3,2;5,4]
x=rand(1,4)
y=3
whos

%% Symbolic variables
syms x a
y = a*cos(2*x);
dy_by_dx = diff(y,x)
Integral_y = int(y,x)
whos x y dy_by_dx Integral_y

%% Complex numbers
clc
b=2-j
m=3+4j
p=b^2/m
F=[b,m;p,0]
imag_part_of_F=imag(F)
whos b m p F imag_part_of_F

%% Logical variables
clc
yes = logical(1)
no=logical(0)
yes_or_no = or(yes,no)
yes_and_no = and(yes,no)
not(no)
x=1;y=2;
ybigger=(y>x)
ysmaller=(y<x)
ysame=(y==x) %%% or isequal(y,x)
z=[1 1 0 0 1]
z=logical(z)
p=[1 2 3 4 5];
dispalyp = p(z)
otherp = p(not(z))
whos yes no
%% Introduction to strings
clc;
st1 = 'Hello student'
st2 = 'Goodbye'
ex = [st1(7),st2(1)]
num = num2str(200)
num2 = '345'
num3 = str2num(num2)
SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 11 of 16
Introduction to MATLAB LAB 01
whos

%% Dates and times


clc;
dd=date
cc=clock
timenow = cc(4:5)
datenow = str2num(dd(1:2))
p1=sprintf('The time is %d hours %d minutes',cc(4),cc(5))
whos dd cc p1
%% formating
clc
format loose % gives spacing between lines
format long %display up to 15 decimal points
x = pi
format short %display up to 4 decimal points
x
format long
x
format bank % display up to 2 decimal points
x
format rat % Rational
x

%% Structure arrays
clc;clear
a=3;
x=[1 2 3];
A=[1 2;0 4];
st='example structure';
z=1+2j;
ALL.a=a;
ALL.x=x;
ALL.A=A;
ALL.st=st;
ALL.z=z
extractstring=ALL.st

Q.w=[23 45];
Q.r=[j,2j,4];
Q.st='Just two vectors'
Q.ALL=ALL

%% Cell arrays
clc;
a=3;
x=[1 2 3];
A=[1 2;0 4];
st='example cell';
z=1+2j;
Example_cell=cell(2,3)
Example_cell{1,1}=x;
Example_cell{1,2}=A;
SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 12 of 16
Introduction to MATLAB LAB 01
Example_cell{2,1}=st;
Example_cell{2,2}=z;
Example_cell{2,3}=Q
whos Example_cell

extractstring=Example_cell{2,1}

%% Function Handles functions


clc
g=@(x) x^2+3
g_at_2=g(2)

k=@(ww) ww^3+2*ww-3
k_at_minus1=k(-1)

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 13 of 16


Introduction to MATLAB LAB 01
TASKS:
Solve these questions in MATLAB and write answers/code in the manual. Submit your manual and
Script files on SLATE before leaving the lab. [ Total Marks = 10]

TASKS:
Q.1 Matrix Manipulation:
A matrix A with magic(4) command is generated as follows: Perform the following operations on
matrix A.

i) Replace the first column of A with following vector, and say it matrix B: [2]
1
-2
3
-4

ii) Swap 1st column of A with 3rd, and 2nd with 4th and say it matrix C. [2]

iii) Delete 4th column from matrix A and name it matrix X. [1]

iv) Write Matlab code that modifies matrix A into D [2].

v) Write Matlab code that modifies matrix A into matrix E. [2].

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 14 of 16


Introduction to MATLAB LAB 01

vi) Write Matlab code that modifies matrix A into F [2].

vii) Write Matlab code that modifies matrix A into G. [2]

viii) Write Matlab code that find the sum of elements of 1st row and 4th row of matrix B and
store the result in variable a and b respectively [2].

ix) Write Matlab code to convert matrix A into row vector 𝐻1 as follows:

x) Use the vector 𝐻1 to make matrix A again.

xi) Extract the diagonal entries of matrix A and use them to make a diagonal matrix as
follows:

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 15 of 16


Introduction to MATLAB LAB 01

xii) Write Matlab command to find the total elements in matrix A?

Useful Hints: - Above tasks can be completed by understanding following commands:


fliplr (), flipud (), reshape (), diag (), sort (), vec2mat (), numel () etc. Use Matlab help or explore
these commands on Internet.

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 16 of 16

You might also like