0% found this document useful (0 votes)
48 views

GSF Matlab Plotting

This document provides an overview of basic and advanced plotting techniques in Matlab. It discusses how to generate simple 2D plots, add titles, labels, and legends. It also covers generating subplots with multiple plots, 3D surface plots, and lists additional Matlab resources for more detailed plotting help.

Uploaded by

Ivan Bevanda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

GSF Matlab Plotting

This document provides an overview of basic and advanced plotting techniques in Matlab. It discusses how to generate simple 2D plots, add titles, labels, and legends. It also covers generating subplots with multiple plots, 3D surface plots, and lists additional Matlab resources for more detailed plotting help.

Uploaded by

Ivan Bevanda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Plotting in Matlab Page 1 Basics of Plotting in Matlab GSF 3/22/12 Table of Contents Basic Overview o Syntax o Labeling Axes

s o Legends Manipulating Axes Subplots Multiple Y-Axes Statistics 3-D plots Additional Resources

Basic Overview To use the plot function in Matlab, you should first make sure that the matrices/vectors you are trying to use are of equal dimensions. For example, if I wanted to plot vector X = [3 9 27] over time, my vector for time would also need to be a 1x3 vector (i.e. t = [1 2 3]). Syntax To plot the example vectors above in a new figure: clear all % clear all previous variables

X = [3 9 27]; % my dependent vector of interest t = [1 2 3]; % my independent vector figure plot(t, X) Labeling Axes To give the above figure a title and axis labels: title(Plot of Distance over Time) % title ylabel(Distance (m)) % label for y axis xlabel(Time (s)) % label for x axis % create new figure

Plotting in Matlab Page 2

Legends If you have plotted multiple dependent vectors on the same plot and want to distinguish them from each other via a legend, the syntax is very similar to the axis labeling above. It is also possible to set colors for the different vectors and to change the location of the legend on the figure. Example: clear all X = [3 9 27]; Y = [10 8 6]; Z = [4 4 4]; t = [1 2 3]; figure hold on % dependent vectors of interest

% independent vector % allow all vectors to be plotted in same % figure

plot(t, X, blue, t, Y, red, t, Z, green) title(Plot of Distance over Time) % title ylabel(Distance (m)) % label for y axis xlabel(Time (s)) % label for x axis legend(Trial 1, Trial 2, Trial 3) legend(Location,NorthWest) % move legend to upper left

Plotting in Matlab Page 3

Subplots It can sometimes be useful to display multiple plots on the same figure for comparison. This can be done using the subplot function, that takes arguments for number of rows of plots, number of columns of plots, and plot number currently being plotted: Example: clear all close all % subplot (nrows,ncols,plot_number) x=0:.1:2*pi; subplot(2,2,1); plot(x,sin(x)); subplot(2,2,2); plot(x,cos(x)); subplot(2,2,3) plot(x,exp(-x)); subplot(2,2,4); plot(x, x.^3); % x vector from 0 to 2*pi, dx = 0.1 % plot sine function % plot cosine function % plot negative exponential function % plot x^3

Plotting in Matlab Page 4

Plotting in 3-D There are also ways to plot in multiple dimensions in Matlab*. One type of 3-D plot that may be useful is a surface plot, which requires you to generate some kind of x-y plane and then apply a 3rd function as the z dimension. Example: clear all close all [x,y] = meshgrid([-2:.2:2]); Z = x.*exp(-x.^2-y.^2); figure surf(x,y,Z,gradient(Z)) colorbar % % % % % set up 2-D plane % plot 3rd dimension on plane

surface plot, with gradient(Z) determining color distribution display color scale, can adjust location similarly to legend

Plotting in Matlab Page 5

* See reference material for a more detailed description of 3-D plotting and its applications. Additional Matlab References In Matlab, type help (insert phrase) to get description of command functionality Ex. help plot gives instructions for what arguments to pass the plot function, how to use it, what output to expect, etc. Matlab website: http://www.mathworks.com/help/techdoc/ Useful Matlab functions: http://www.math.ufl.edu/help/matlab-tutorial/matlabtutorial.html http://www.math.ucsd.edu/~bdriver/21d-s99/matlab-primer.html * Note the Graphics section for help with plotting

You might also like