tags:

views:

262

answers:

2

What's the difference between function plot and line in matlab? Are they doing the same thing? Thanks!

+2  A: 

plot() is used to create a graphic, usually a line graph of some sort. line() creates a lin object which may appear in, say, a graphic. No they're not doing the same thing. I'd usually use plot for creating a graphic, line for adding lines to an existing graphic.

If this doesn't answer your question, have a look at the documentation which covers these matters in great detail.

High Performance Mark
+5  A: 

The functions PLOT and LINE do nearly the same thing, but PLOT is a high-level function that may have more interaction with other graphics objects. For example, from the documentation for LINE:

Unlike high-level functions such as plot, line does not respect the settings of the figure and axes NextPlot properties. It simply adds line objects to the current axes. However, axes properties that are under automatic control, such as the axis limits, can change to accommodate the line within the current axes.

This documentation on core graphics objects in MATLAB has more information. A line is listed as a core graphics object, and the function LINE is the low-level function used to create one. High-level functions like PLOT are likely internally calling the LINE function to create the graphics. Here's an excerpt from the documentation on core graphics objects discussing in more detail high-level versus low-level functions:

Many MATLAB graphics functions call the object creation functions to draw graphics objects. However, high-level routines also clear the axes or create a new figure, depending on the settings of the axes and figure NextPlot properties.

In contrast, core object creation functions simply create their respective graphics objects and place them in the current parent object. They do not respect the settings of the figure or axes NextPlot property.

For example, if you call the line function,

line('XData',x,'YData',y,'ZData',z,'Color','r')

MATLAB draws a red line in the current axes using the specified data values. If there is no axes, MATLAB creates one. If there is no figure window in which to create the axes, MATLAB creates it as well.

If you call the line function a second time, MATLAB draws the second line in the current axes without erasing the first line. This behavior is different from high-level functions like plot that delete graphics objects and reset all axes properties (except Position and Units). You can change the behavior of high-level functions by using the hold command or by changing the setting of the axes NextPlot property.

gnovice

related questions