CL2014
MATLAB Programming
Lecture: 05
Course Instructor
Engr Muhammad Umar Khan Niazi
Office: C1-21, Ext.: 218, Email: umar.niazi@nu.edu.pk
1
2 Plots in MATLAB
Graphs (in 2-D) are drawn with the plot statement.
In its simplest form, it takes a single vector argument as in plot(y).
In this case the elements of y are plotted against their indexes, e.g.,
plot(rand(1, 20)) plots 20 random numbers against the integers
1–20, joining successive points with straight lines.
3 Plots in MATLAB
Probably the most common form of plot is plot(x, y) where x and y are
vectors of the same length, e.g.,
x = 0:pi/40:4*pi;
plot(x, sin(x))
4 Plots in MATLAB
MATLAB has a set of ‘easy-to-use’ plotting commands, all starting with
the string ‘ez’. The easy-to-use form of plot is ezplot, e.g.,
ezplot(’tan(x)’)
5 Plots in MATLAB
MATLAB has a set of ‘easy-to-use’ plotting commands, all starting with
the string ‘ez’. The easy-to-use form of plot is ezplot, e.g.,
ezplot(’tan(x)’)
6 Plots in MATLAB
Graphs may be labeled with the following statements:
gtext(’text’)
Writes a string (’text’) in the graph window. gtext puts a cross-hair in
the graph window and waits for a mouse button or keyboard key to be
pressed. The cross-hair can be positioned with the mouse or the arrow
keys. For example,
gtext( ’X marks the spot’ )
7 Graph Annotation
8 Plots in MATLAB
Grid adds/removes grid lines to/from the current graph. The grid state
may be toggled
text(x, y, ’text’) writes text in the graphics window at the
point specified by x and y. If x and y are vectors, the text is written at
each point.
title(’text’) writes the text as a title on top of the graph.
xlabel(’horizontal’) labels the x-axis.
ylabel(’vertical’) labels the y-axis
9 Plots in MATLAB
To plot x against y
x 1 2 3 5 7 7.5 8 10
y 2 6.5 7 7 5.5 4 6 8
>> x=[1 2 3 5 7 7.5 8 10];
>> y=[2 6.5 7 7 5.5 4 6 8];
>> plot(x,y)
Once the plot command is executed, the Figure Window opens with the
following plot.
10 Plots in MATLAB
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’)
Line Specifier Line Specifier Marker Specifier
Style Color Type
Solid - red r plus sign +
dotted : green g circle o
dashed -- blue b asterisk *
dash-dot -. Cyan c point .
magenta m square s
yellow y diamond d
black k
11 Plots in MATLAB
Year 1988 1989 1990 1991 1992 1993 1994
Sales (M) 127 130 136 145 158 178 211
>> year = [1988:1:1994];
>> sales = [127, 130, 136, 145, 158, 178, 211];
>> plot(year,sales,'--r*')
12 Plots in MATLAB
x = -pi:pi/10:pi;
y = tan(sin(x)) - sin(tan(x));
figure
plot(x,y,'--
gs','LineWidth',2,'MarkerSize',10,'MarkerEdgeColor','b','MarkerFace
Color',[0.5,0.5,0.5])
13 Plots in MATLAB
subplot(2,2,1)
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
title('Subplot 1: sin(x)')
subplot(2,2,2)
y2 = sin(2*x);
plot(x,y2)
title('Subplot 2: sin(2x)')
subplot(2,2,3)
y3 = sin(4*x);
plot(x,y3)
title('Subplot 3: sin(4x)')
subplot(2,2,4)
y4 = sin(8*x);
plot(x,y4)
title('Subplot 4: sin(8x)')