Plotting data is one of the most important tasks in Matlab programming. In this matlab programming tutorial pdf, we will show you how to plot data, modify plots and save your work. Here is an example of a plot of a sine wave.
Try to enter the following lines of code into your Matlab Command Window. Plotting multiple sets of data on the same axes is a useful feature of Matlab. The hold command allows users to add multiple plots to the same axis. The hold on command can be used in place of hold all. However, if the hold on command is used, then the subsequent plots will be the default blue color with a solid line, unless another color and linestyle are specified. For this reason, the hold all command is better for most applications.
There are times when putting multiple axes on the same figure helps to display results more clearly. The following command breaks the current figure up into m rows and n columns, and creates axes in the pth division. In the following example, a set of axes will be created in the third division of the 2 x 2 area in the figure. Subplot counts the divisions first along rows from left to right, then along columns from up to down. This code will create a plot in the upper-right corner of the figure.
Each set of axes has its own properties, labels, title and legend. The legend function in Matlab creates a legend in the current axes of the current figure. This function is useful for labeling multiple plots on the same axes. This command will, by default, create a text box in the upper-right corner of the axes that shows both lines and their labels.
Sometimes the default placement of the legend interferes with the data in the plot and another location is desirable. The grid function allows a Matlab user to draw vertical and horizontal lines across the plot at intervals specified by the axis tick marks. The syntax for this command is quite simple. Line properties can be modified in two different ways, using the plot command or using the set command. This section of the tutorial will use code that builds on Example 1. This command will allow you to change the color of the plotted line. Alternatively, the line color could have been set in the plot command.