Professional Documents
Culture Documents
(EL-223)
LABORATORY MANUAL
Spring 2020
________________________________________________________________________________________________________________________________________________________
NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES, ISLAMABAD
PLOTTING
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.
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.
• Once the plot command is executed, the Figure Window must open with the following plot
as shown in “Fig 2.3”
“Fig 2.3”
plot(x,y,’line specifiers’)
Marker Specifier:
Color Specifiers:
• 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,’*’) 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.
“Fig 2.4”
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.
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.
“Fig 2.5”
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”
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
Subplots:
• Subplot divides the current figure into rectangular panes that are numbered row wise.
• Syntax:
subplot(rows,cols,index)
“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”
Run File
After setting breakpoints, run the file from the Command Window or the Editor. Running the file
produces these results:
• 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: