MATLAB Tips
Comments or problems: Contact Dr. Timothy D. Placek
All Pages Copyright 2002 by Auburn University, Auburn, AL 36849

 More about Plotting I By default, MATLAB creates an "adequate" plot when you just use the plot routine. In this case, one gets a plot developed with default attributes and no labels or titles. Of course, one can then go into the plot window and "improve" the plot by "tweaking" the various elements of the plot and axis. The drawback in doing this is that everything you do to the graph applies only to that particular graph so if you emplot the plot routine again, you will once more have to "tweak" the graph. There are many subcommands for the plot command. These can be typed in following the plot command which produces the plot or placed in an "m-file" and used whenever desired. In this tutorial, we will use the first method. As a first example, we will plot the sin(x) and use subcommands to add axis legends and graph titles. >> x=0:0.05:2*pi; >> h=plot(x,sin(x)); >>% set line width and line style ("h" is a "handle" which points to the graph just produced) >> set(h,'LineWidth',2); >> set(h,'LineStyle','--'); >> set(h,'Color','r'); >>% set axis and graph titles >> xlabel('x-values'); >> ylabel('sin(x)'); >> title('Plot of x vs sin(x)'); >>% set grid lines on and select axes limits >> grid on; >> axis([0, 2*pi, -1.5, 1.5]); >> % set legend for function >> legend('sin(x)');   As a second example, we will plot the sin(x) and cos(x) on the same plot and use subcommands to add axis legends and graph titles. >> x=0:0.05:2*pi; >> h=plot(x,sin(x),x,cos(x)); >>% set line width and line style and color >> set(h,{'LineWidth'},{2;2}); >> set(h,{'LineStyle'},{'--';'-'}); >> set(h,'{Color'},{'r';'b'}); >>% set axis and graph titles >> xlabel('x-values'); >> ylabel('function-values'); >> title('Plot of x vs sin(x)and cos(x)'); >>% set grid lines on and select axes limits >> grid on; >> axis([0, 2*pi, -1.5, 1.5]); >> % set legend for function >> legend({'sin(x)';'cos(x)'});   Notice that when there are two or more plots on one graph, one uses "set" notation with elements separated by semicolons to provide the values. This is demonstrated above in the legend and the characteristics of each plot line. More about plotting options will be discussed in the next "MATLAB Tip".

Comments or problems: Contact Dr. Timothy D. Placek
All Pages Copyright 2002 by Auburn University, Auburn, AL 36849