back.gif MATLAB Tutorial next.gif (277 bytes)


 

5. Plotting

Commands covered:

The command most often used for plotting is plot, which creates linear plots of vectors and matrices; plot(t,y) plots the vector t on the x-axis versus vector y on the y-axis. There are options on the line type and the color of the plot which are obtained using plot(t,y,'option'). The linetype options are '-' solid line (default), '--' dashed line, '-.' dot dash line, ':' dotted line. The points in y can be left unconnected and delineated by a variety of symbols: + . * o x. The following colors are available options:

 r  red
 b  blue
 g  green
 w  white
 k  black

For example, plot(t,y,'--') uses a dashed line, plot(t,y,'*') uses * at all the points defined in t and y without connecting the points, and plot(t,y,'g') uses a solid green line. The options can also be used together, for example, plot(t,y,'g:') plots a dotted green line.

To plot two or more graphs on the same set of axes, use the command plot(t1,y1,t2,y2), which plots y1 versus t1 and y2 versus t2.

To label your axes and give the plot a title, type

Finally, add a grid to your plot to make it easier to read. Type

grid

The problem that you will encounter most often when plotting functions is that MATLAB will scale the axes in a way that is different than you want them to appear. You can easily override the autoscaling of the axes by using the axis command after the plotting command:

axis([xmin xmax ymin ymax]);

where xmin, xmax, ymin, and ymax are numbers corresponding to the limits you desire for the axes. To return to the automatic scaling, simply type axis.

For discrete-time signals, use the command stem which plots each point with a small open circle and a straight line. To plot y[k] versus k, type stem(k,y). To obtain filled circles, as the plots appear in the textbook, use the command stem(k,y,'filled').

To plot more than one graph on the screen, use the command subplot(mnp) which partitions the screen into an mxn grid where p determines the position of the particular graph counting the upper left corner as p=1. For example,

plots the bode plot with the log-magnitude plot on top and the phase plot below. Titles and labels can be inserted immediately after the appropriate semilogx command or plot command. To return to a full screen plot, type subplot(111).


 

6. Loading and Saving Data

When using MATLAB, you may wish to leave the program but save the vectors and matrices you have defined. To save the file to the working directory, type

save filename

where "filename" is a name of your choice. To retrieve the data later, type

load filename


back.gif MATLAB Tutorial Table of Contents

This tutorial is available as a supplement to the textbook Fundamentals of Signals and Systems Using Matlab by Edward Kamen and Bonnie Heck, published by Prentice Hall. The tutorial is designed for students using either the professional version of MATLAB (ver. 4.0) with the Control Systems Toolbox (ver. 3.0) and the Signal Processing Toolbox (ver. 2.0), or using the Student Edition of MATLAB (ver. 4.0). For more information on MATLAB, contact The Mathworks, Inc.