Getting Started Previous page   Next Page

Examples -- Using MATLAB Plotting Tools

Suppose you want to graph the function y = x3 over the x domain -1 to 1. The first step is to generate the data to plot.

It is simple to evaluate a function because MATLAB can distribute arithmetic operations over all elements of a multivalued variable.

For example, the following statement creates a variable x that contains values ranging from -1 to 1 in increments of 0.1 (you could also use the linspace function to generate data for x). The second statement raises each value in x to the third power and stores these values in y:

Now that you have generated some data, you can plot it using the MATLAB plotting tools. To start the plotting tools, type

MATLAB displays a figure with plotting tools attached.

Plotting Two Variables

A simple line graph is a suitable way to display x as the independent variable and y as the dependent variable. To do this, select both variables (click to select, then Shift-click to select again), then right-click to display the context menu.

Select plot(x,y) from the menu. MATLAB creates the line graph in the figure area. The black squares indicate that the line is selected and you can edit its properties with the Property Editor.

Changing the Appearance

Next change the line properties so that the graph displays only the data point. Use the Property Editor to set following properties:

Adding More Data to the Graph

You can add more data to the graph by defining more variables or by specifying an expression that MATLAB uses to generate data for the plot. This second approach makes it easy to explore variations of the data already plotted.

To add data to the graph, select the axes in the Plot Browser and click the Add Data button. When you are using the plotting tools, MATLAB always adds data to the existing graph, instead of replacing the graph, as it would if you issued repeated plotting commands. That is, the plotting tools are in a hold on state.

The picture above shows how to configure the Add Data to Axes dialog to create a line plot of y = x4, which is added to the existing plot of y = x3.

Changing the Type of Graph

The plotting tools enable you to easily view your data with a variety of plot types. The following picture shows the same data as above converted to stem plots. To change the plot type,

  1. Select the plotted data in the Plot Browser.
  2. Select Stem in the Plot Type menu.


Previous page  Editing Plots Modifying the Graph Data Source Next page

© 1994-2005 The MathWorks, Inc.