SNS Lab 02 SP 20
SNS Lab 02 SP 20
SNS Lab 02 SP 20
(EL-223)
LABORATORY MANUAL
Spring 2020
INTRODUCTION TO MATLAB II
(LAB # 02)
Student Name: ______________________________________________
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.
If you replace above t as t=linspace (0,8,801); you will get the same output plot.
%% 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
%% 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}
k=@(ww) ww^3+2*ww-3
k_at_minus1=k(-1)
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]
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:
xi) Extract the diagonal entries of matrix A and use them to make a diagonal matrix as
follows: