Presentation is loading. Please wait.

Presentation is loading. Please wait.

Plotting Signals in MATLAB

Similar presentations


Presentation on theme: "Plotting Signals in MATLAB"— Presentation transcript:

1 Plotting Signals in MATLAB
Lecture 4: Plotting Signals in MATLAB 1st semester By: Elham Sunbu

2 Outline Graph in MatLab 2-D plotting 1. Creating and Type of graph
2. Using Basic Plotting Functions 3. Plotting Multiple graph in same plot 4. Formatting plots

3 Creating Graph in MATLAB
Line Plot Pie Charts, Bar Plots, and Histograms Command Description bar, bar3 Bar graph 2-D, 3-D Pie, pie3 Pie graph 2-D, 3-D hist Histogram plot Command Description Plot, plot3 Create 2-D graph and 3-D plotyy 2-D line plots with y-axes on both left and right side Discrete Graph Command Description Stem, stem3 Plot discrete sequence data 2-D, 3-D stairs Stairstep graph

4 2-D Plotting - Specify x-data and/or y-data - Syntax:
- Specify color, line style and marker symbol (Default values used if ‘clm not specified) - Syntax: Plotting single line: plot(xdata, ydata, 'color_linestyle_marker') Plotting multiple lines: If data is given, the information is entered as the elements of the vectors x and y. If the values of y are determined by a function from the values of x, than a vector x is created first, and then the values of y are calculated for each value of x. The PLOT functions is used to produce 2-dimensional curves, using x- and y-data matrices specified by the user. However, PLOT also accepts character-string arguments that designate various colors, line styles and marker symbols for the resulting line. For example, to produce a red dotted line with square markers at each data point, type: » plot(x, y, 'r:square') You can plot multiple lines at once, using pairs of x- and y-data. As with plotting single lines, the color, line style and marker information can be specified explicitly. However, if not specified, MATLAB automatically cycles through a predefined (but user settable) list of colors to identify each line. For example, the following statement plots three lines as a function of t (First = blue / Second = green / Third = red): » plot(t, y1, t, y2, t, y3) plot(x1, y1, 'clm1', x2, y2, 'clm2', ...)

5 Line Specification Three components can be specified in the string specifiers along with the plotting command. They are: Line style Marker symbol Color iers Meaning '-' Solid line (default) '--' Dashed line ':' Dotted line '-.' Dash-dot line

6 Line Specification Meaning Color Specifiers Blue b Cyan c Green g
Black k Magenta m Red r Yellow y Meaning Marker down v circle o triangle left < diamond d triangle right > hexagram h up triangle ^ pentagram p x -mark x plus + point . square s star *

7 2-D Plotting - Example » x = 0:.1:2*pi; » y = sin(x); » plot(x,y)
- Create a Blue Sine Wave » x = 0:.1:2*pi; » y = sin(x); » plot(x,y) In this example, the data plotted along the x-axis is defined as: » x=0:0.1:2*pi; The data plotted along the y-axis is defined as: » y=sin(x); Plot these data as follows (default = blue, solid line, no marker): » plot(x,y)

8 Adding a Grid - GRID ON creates a grid on the current figure - GRID OFF turns off the grid from the current figure - GRID toggles the grid state grid on

9 Controlling the Axes - Setting Axis Limits & Grids The axis command lets you to specify your own limits: axis([xmin xmax ymin ymax]) You can use the axis command to make the axes visible or invisible: axis on / axis off The grid command toggles grid lines on and off: grid on / grid off

10 Using Basic Plotting Functions
- The following commands are useful when plotting: Note that all text must be put within ' '. Graphing functions MATLAB command Label the horizontal axis. xlabel('text') Label the vertical axis. ylabel('text') Attach a title to the plot. title('text') Change the limits on the x and y axis. axis([xmin xmax ymin ymax]) "Keep plotting in the same window." hold on Turn off the "keep-plotting-in-the-same-window-command". hold off using the strings to label various curves legend(‘string1’,’string2’,’string3’) Places the string (text) on the plot at coordinate x,y relative to the plot axes. text(x,y,’string’) The title (), xlabel (), and ylabel () commands take a string argument in single quotes in order to label the top of the plot, the x-axis, and the y-axis, respectively. The axis () command takes a 2 or 4 element vector as its argument where the minimum and maximum x values are set or the minimum and maximum x and y values are set. Matlab will probably complain loudly if either xmax or ymax is less than xmin or ymin. The use of the axis () command allows the user to override the default setting that Matlab would otherwise pick. It can be use to look closely at a small window of data which would otherwise be unclear if a lot of points where available. It can be particularly useful when making an interactive plot that needs to resize itself. The legend () command allows us to place a legend on a plot. The legend () command takes as its arguments a list of strings enclosed in single quotes that are the explanatory information you would like displayed next to a short sample of the line and data point display for a given set of data. The order of the strings must correspond to the order of the graphs that were added to the plot. In addition to the string, the legend () command can take a position argument that dictates where the legend is to be placed on the plot. The default is the upper right corner of the plot. The text () command takes three arguments: an x and y location to begin writing a string and the text string that you would like displayed. The string must be enclosed in single quotes. The x and y location must be supplied in the same scale that is being used for the x and y axes. In other words, the location of the text string is relative to the plot. Fixed screen locations do not exist.

11 Adding Additional Plots to a Figure
PLOTTING MULTIPLE GRAPHS IN THE SAME PLOT Plotting two (or more) graphs in one plot: Using the plot command. 2. Using the hold on, hold off commands. In addition to supplying multiple vector pairs to the plot () command, we can create graphs with multiple plots by using the hold on/hold off commands. Once a plot () command has been issued and hold on specified, all subsequent plot () commands will be plotted on the initial graph. Axis properties will be maintained for subsequent plots. If line specifiers are not specified in the subsequent plots, Matlab will use the default order and colors when drawing additional plots on the graph. To return to the default situation in which each plot () command causes a new plot to be drawn in the graph window, the hold off command must be issued.

12 Subplots

13 Formatting Plots - A plot can be formatted to have a required appearance. - With formatting you can: Add title to the plot. Add labels to axes. Change range of the axes. Add legend. Add text blocks. Add grid.

14 Thank You


Download ppt "Plotting Signals in MATLAB"

Similar presentations


Ads by Google