You are on page 1of 12

Signals and Systems

(EL-223)

LABORATORY MANUAL
Spring 2020

Dr. Shahzad Saleem


Engr. Fakhar Abbas
Plotting and Debugging in Matlab
(LAB # 04)
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: 2.01


Verified by: Dr. Waqas bin Abbas, Dr. Shahzad Saleem

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 1 of 12


Plotting and Debugging in MATLAB LAB 4
Lab # 04: Plotting and Debugging in MATLAB
Objective:
In this lab, you will learn:
Two dimensional plot
Plot formatting
Displaying multiple plots
Debugging a code in MATLAB environment.

Tools Used: MATLAB

PLOTTING

TWO-DIMENSIONAL PLOT() COMMAND

plot (x,y)

• where x is a vector (one dimensional array), and y is a vector. Both vectors must have the
same number of elements.

• The plot command creates a single curve with the x values on the abscissa (horizontal axis)
and the y values on the ordinate (vertical axis).

• The curve is made from segments of lines that connect the points that are defined by the x
and y coordinates of the elements in the two vectors.

PLOT OF GIVEN DATA:

x 1 2 3 5 7 7.5 8 10

y 2 6.5 7 7 5.5 4 6 8

• The code can be done in the Command Window, or by writing and then running a script
file.

Code to plot above data:

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


Plotting and Debugging in MATLAB LAB 4

• Once the plot command is executed, the Figure Window must open with the following plot
as shown in “Fig 2.3”

“Fig 2.3”

LINE SPECIFIERS IN THE PLOT() COMMAND:

Line specifiers can be added in the plot command to:


• Specify the style of the line.
• Specify the color of the line.
• Specify the type of the markers (if markers are desired).

plot(x,y,’line specifiers’)

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


Plotting and Debugging in MATLAB LAB 4
The following tables lists the line specifiers:
Line Style Specifiers:

Specifier Line Style


- Solid line (default)
-- Dashed line
: Dotted line
-. Dash dot line

Marker Specifier:

Specifier Marker Type


+ plus sign
O Circle
* Asterisk
. dot
s square
d diamond

Color Specifiers:

Specifier Line Color


r red
g green
b blue
c cyan
m magenta
y yellow
k black

• The specifiers are typed inside the plot() command as strings.

• Within the string the specifiers can be typed in any order.

• The specifiers are optional. This means that none, one, two, or all the three can be included
in a command.

Example:
• plot(x,y) A solid blue line connects the points with no markers.

• plot(x,y,’r’) A solid red line connects the points with no markers.

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


Plotting and Debugging in MATLAB LAB 4

• plot(x,y,’--y’) A yellow dashed line connects the points.

• plot(x,y,’*’) The points are marked with * (no line between the points.)

• plot(x,y,’g:d’) A green dotted line connects the points which are marked with
diamond markers.

Plot of given data using different line specifiers in the plot ( ) command. An example is shown in
Fig. 2.4.

Year 1988 1989 1990 1991 1992 1993 1994

Sales 127 130 136 145 158 178 211

“Fig 2.4”

Formatting the Plots:


A plot can be formatted to have a required appearance.

With formatting you can:

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


Plotting and Debugging in MATLAB LAB 4
• Add title to the plot.
• Add labels to axes.
• Change range of the axes.
• Add legend.
• Add text blocks.
• Add grid.

Formatting Commands:
title(‘string’)
Adds the string as a title at the top of the plot.

xlabel(‘string’)
Adds the string as a label to the x-axis.

ylabel(‘string’)
Adds the string as a label to the y-axis.

axis([xmin xmax ymin ymax])


Sets the minimum and maximum limits of the x- and y-axes.

legend(‘string1’,’string2’,’string3’)
Creates a legend using the strings to label various curves (when several curves are
in one plot). The location of the legend is specified by the mouse.

text(x,y,’string’)
Places the string (text) on the plot at coordinate x,y relative to the plot axes.

Example of Formatted Plot:

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


Plotting and Debugging in MATLAB LAB 4
x=[0:0.1:2*pi];
y=sin(x);
z=cos(x);
plot(x,y,x,z)
title('Sample Plot','fontsize',14);
xlabel('X values','fontsize',14);
ylabel('Y values','fontsize',14);
legend('Y data','Z data')
grid on

“Fig 2.5”

Displaying the Multiple Plots:

Three typical ways to display multiple curves in MATLAB (other combinations are possible…)
One figure contains one plot that contains multiple curves o Requires
the use of the command “hold” (see MATLAB help)

One figure contains multiple plots, each plot containing one curve o
Requires the use of the command “subplot”

Multiple figures, each containing one or more plots, each containing


one or more curves o Requires the use of the command “figure” and
possibly “subplot”

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


Plotting and Debugging in MATLAB LAB 4

One Plot Contains Multiple Curves:

Example:

x=[0:0.1:2*pi];
y=sin(x);
z=cos(x);
plot(x,y,x,z)
grid on

Or

x=[0:0.1:2*pi];
y=sin(x);
z=cos(x);
plot(x,y,’b’)
hold on
Plot(x,z,’g’)
hold off
grid on

• The plot figure produced by above code is shown in “Fig 2.5”.

Subplots:

• Subplot divides the current figure into rectangular panes that are numbered row wise.
• Syntax:

subplot(rows,cols,index)

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


Plotting and Debugging in MATLAB LAB 4
»subplot(2,2,1);
»…
»subplot(2,2,2)
» ...
»subplot(2,2,3)
» ...
»subplot(2,2,4)
» ...

“Fig 2.6”

Example of Subplot:
x=[0:0.1:2*pi];
y=sin(x);
z=cos(x);
subplot(1,2,1);
plot(x,y)
subplot(1,2,2)
plot(x,z)
grid on

“Fig 2.7”

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


Plotting and Debugging in MATLAB LAB 4
Multiple Figures:
Example:
x=[0:0.1:2*pi];
y=sin(x);
z=cos(x);
figure;
plot(x,y)
figure;
plot(x,z)
grid on

Debug a MATLAB Program


To debug your MATLAB® program graphically, use the Editor/Debugger. Alternatively, you can
use debugging functions in the Command Window. Both methods are interchangeable.
Before you begin debugging, make sure that your program is saved and that the program and any
files it calls exist on your search path or in the current folder.
• If you run a file with unsaved changes from within the Editor, then the file is automatically saved
before it runs.
• If you run a file with unsaved changes from the Command Window, then MATLAB software runs
the saved version of the file. Therefore, you do not see the results of your changes.
Set Breakpoint
Set breakpoints to pause the execution of a MATLAB file so you can examine the value or variables
where you think a problem could be. You can set breakpoints using the Editor, using functions in the
Command Window, or both.
There are three different types of breakpoints: standard, conditional, and error. To add
a standard breakpoint in the Editor, click the breakpoint alley at an executable line where you want
to set the breakpoint. The breakpoint alley is the narrow column on the left side of the Editor, to the
right of the line number. Executable lines are indicated by a dash ( — ) in the breakpoint alley. For
example, click the breakpoint alley next to line 2 in the code below to add a breakpoint at that line.

Run File
After setting breakpoints, run the file from the Command Window or the Editor. Running the file
produces these results:

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


Plotting and Debugging in MATLAB LAB 4
• Then press the Run.
• The prompt in the Command Window changes to K>> indicating that MATLAB is in debug mode
and that the keyboard is in control.
• MATLAB pauses at the first breakpoint in the program. In the Editor, a green arrow just to the right
of the breakpoint indicates the pause. The program does not execute the line where the pause occurs
until it resumes running. For example, here the debugger pauses before the program executes x =
ones(1,10);

• MATLAB displays the current workspace in the Function Call Stack, on the Editor tab in
the Debug section.
Pause a Running File
To pause the execution of a program while it is running, go to the Editor tab and click
the Pause button. MATLAB pauses execution at the next executable line, and the Pause button
changes to a Continue button. To continue execution, press the Continue button.
Pausing is useful if you want to check on the progress of a long running program to ensure that it is
running as expected.
Find and Fix a Problem
While your code is paused, you can view or change the values of variables, or you can modify the
code.
View or Change Variable While Debugging
View the value of a variable while debugging to see whether a line of code has produced the expected
result or not. To do this, position your mouse pointer to the left of the variable. The current value of
the variable appears in a data tip.

The data tip stays in view until you move the pointer. If you have trouble getting the data tip to
appear, click the line containing the variable, and then move the pointer next to the variable. For
more information, see Examine Values While Debugging.
You can change the value of a variable while debugging to see if the new value produces expected
results. With the program paused, assign a new value to the variable in the Command Window,
Workspace browser, or Variables Editor. Then, continue running or stepping through the program.
For example, here MATLAB is paused inside a for loop where n = 2:

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 11 of 12


Plotting and Debugging in MATLAB LAB 4

• Type n = 7; in the command line to change the current value of n from 2 to 7.


• Press Continue to run the next line of code.
MATLAB runs the code line x(n) = 2 * x(n-1); with n = 7.

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

You might also like