MATLAB Examples

Section 8 - Visualizing Data

You can create a PDF or HTML version of this MATLAB file by choosing opening it in the MATLAB Editor and choosing "File > Publish ..."

Contents

Line Plots in Two Dimensions

simple_x_points = [1 2 3 4 5]
simple_x_points =

     1     2     3     4     5

simple_y_points = [25 0 20 5 15]
simple_y_points =

    25     0    20     5    15

plot(simple_x_points, simple_y_points);
plot(simple_y_points, simple_x_points);
plot(simple_x_points, simple_y_points);
xlabel('this is text describing the x-axis');
ylabel('this is text describing the y-axis');
title('this is text giving a title for the graph');
grid on;
x_points = [-5 : .1 : 5];
y_points = x_points .^ 2;
plot(x_points,y_points);
xlabel('x-axis'); ylabel('y-axis'); title('A Parabola');
grid on

Multiple Plots and Subplots

x_points = [-10 : .05 : 10];
plot(x_points, exp(x_points));
grid on
hold on
plot(x_points, exp(.95 .* x_points));
plot(x_points, exp(.85 .* x_points));
plot(x_points, exp(.75 .* x_points));
xlabel('x-axis'); ylabel('y-axis');
title('Comparing Exponential Functions');
x_points = [-10 : .05 : 10];
line = 5 .* x_points;
parabola = x_points .^ 2;
exponential = exp(x_points);
absolute_value = abs(x_points);
subplot(2,2,1);plot(x_points,line);
title('Here is the line');
subplot(2,2,2);plot(x_points,parabola);
title('Here is the parabola');
subplot(2,2,3);plot(x_points,exponential);
title('Here is the exponential');
subplot(2,2,4);plot(x_points,absolute_value);
title('Here is the absolute value');

Line Plots in Three Dimensions

clf; % clear the figure
X = [10 20 30 40];
Y = [10 20 30 40];
Z = [0 210 70 500];
plot3(X,Y,Z); grid on;
xlabel('x-axis'); ylabel('y-axis'); zlabel('z-axis');
title('Pretty simple');
Z = [0 : pi/50 : 10*pi];
X = exp(-.2.*Z).*cos(Z);
Y = exp(-.2.*Z).*sin(Z);
plot3(X,Y,Z); grid on;
xlabel('x-axis'); ylabel('y-axis'); zlabel('z-axis');...
title('A little more interesting!');

Three-Dimensional Surface Mesh Plots

x_points = [-10 : 1 : 10];
y_points = [-10 : 4 : 10];
[X, Y] = meshgrid(x_points,y_points);
Z = X.^2 + Y.^2;
mesh(X,Y,Z); xlabel('x-axis'); ...
ylabel('y-axis');zlabel('z-axis');