0% found this document useful (0 votes)
18 views40 pages

1st - Yr - Lecture 08 - NEW

This document discusses saving and plotting data in MATLAB. It covers: 1) Saving MATLAB data files using the "save" command to store variables for later use. 2) Creating 2D graphs of data using the "plot" command, including options to customize colors, symbols, and line styles. 3) Creating preliminary 3D line plots of data in MATLAB.

Uploaded by

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

1st - Yr - Lecture 08 - NEW

This document discusses saving and plotting data in MATLAB. It covers: 1) Saving MATLAB data files using the "save" command to store variables for later use. 2) Creating 2D graphs of data using the "plot" command, including options to customize colors, symbols, and line styles. 3) Creating preliminary 3D line plots of data in MATLAB.

Uploaded by

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

ENG 1060

Computing for Engineers

Lecture 8:
Saving your results / Graphical output
Last Lecture

• Last lecture several key concepts were


covered which are required to program
using MATLAB.

– Relational operators
– Logical operators
– If-else-elseif constructs
– for loops
– while loops
Lecture Outline
• Today, we consider three topics:
– Using MATLAB Data files
– Creating 2-D Graphs of your data
– Creating 3-D Graphs of your data

At the end of this lecture you will


have been introduced to all of these
topics.
An Engineering Example…
• The Great Red Spot is an
anticyclonic storm on the planet
Jupiter, 22° south of the equator,
which has lasted at least 340
years.
• It is large enough to contain two
or three planets of Earth size.
• It was first observed either by
Cassini or Hooke around 1665.
An Engineering Example…
• Scientists have modeled Jupiter’s giant red
spot in order to understand the underlying
weather patterns. This understanding may
assist meteorological models here on Earth.

This plot provides a


three-dimensional
visualization of a
simulation of Jupiter
´s Great Red Spot,
defined by an outer
isothermal surface.
MATLAB data files
• You’ve written a large M-file for an assignment (or for
your boss at work). You need to save all your data for
later use, how do you do this?

The variables in the MATLAB workspace can be saved in a


format native to matlab using the save command, for example:

>>save

Stores all variables in the matlab workspace, in the MATLAB


binary format, and in the file matlab.mat in the current directory.

• Native binary *.mat files save the variables in double


precision format, and save the names of the variables.
MATLAB data files
• The save command can be used to store specific
variables as well, for example:

>>save (‘var1’, ‘var2’, ‘var3’)


saves the variables var1, var2 and var3 to matlab.mat.

• You can also save to your filename of choice, for


example:
>>save (‘storage_file’, ‘var1’, ‘var2’, ‘var3’)

saves the variables var1, var2 and var3 to


storage_file.mat.
MATLAB data files
By default, MATLAB stores the *.mat file in binary
format. It is therefore not possible to read it using
a standard text editor, or to load it into a
spreadsheet program.

It is possible to save your variables in ascii format


(readable text), for example:

>>save (‘var1’, ‘var2’, ‘var3’, -ascii)


This saves the file in 8-bit ascii format. There are
other options, type help save for more
information.
MATLAB data files
• Once you have saved your *.mat file, you will at
some point need to load it back into MATLAB. This
is very simple:

>>load

• Loads all variables found in matlab.mat from the


current directory (or from the Matlab path).

• As MATLAB loads from the first file called


matlab.mat it can find, it is better to give your file
an explicit file name when saving.
MATLAB data files
• You can choose to load specific variables from a
*.mat file, you must include the filename to do
this:

>>load (‘storage_file’, ‘var1’, ‘var3’)

• Loads the variables var1 and var3 from the


filename storage_file.mat.

• Note that for this to work, storage_file.mat must


be in the current directory.
• Note: the ‘load’ command can also open ascii data
files.
MATLAB data files
• Finally, it is possible to find out whether a data
file exists and what variables it holds, for
example:

>>exist (‘matlab.mat’, ‘file’)

• Returns 0 if the file doesn’t exist in the local


directory and 2 if it does.
• The statement:

>>whos -file storage_file.mat

• Will list all the variables in the file


‘storage_file.mat’
2D Graphics – The plot function
• The function ‘plot’ is the most common function
for plotting 2-D data.
• ‘plot’ generates a graph of data arrays on
appropriate axes and connects the points with
straight lines. Let’s look at an example:

x = linspace(0,2*pi,30);
y = sin(x);
plot(x,y), title('figure 6.1: Sine Wave')

The function ‘plot’ opens a graphics


window (called a figure window), scales
the axes to fit the data, plots the points
and connects the points with straight
lines.
2D Graphics – The plot function
• As another example, the following snippet of code
plots two functions on the same graph:

x = linspace(0,2*pi,30);
y = sin(x);
z = cos(x)
plot(x,y,x,z), title('figure 6.2: Sine and cos
Wave')

By giving ‘plot’ another pair of


arguments instructs it to generate
a second line.
Plot automatically draws the
second curve in a different colour.
Plot automatically plots as many
curves as it receives pairs of input
arguments.
2D Graphics – The plot function
• What if you change the order of the arguments to plot ?

x = linspace(0,2*pi,30);
y = sin(x);
z = cos(x)
plot(y,x,z,x), title('figure 6.2: Sine and cos Wave')

Note that we have written y as the


independent variable and x as the
dependent variable here. The
same is true for z and x.

This essentially rotates the graph


by 90o
2D Graphics – The plot function
You may alter the linestyle, or have markers (points) instead
of lines – you can also tell MATLAB which colour you want
the plot to be displayed in. The following are optional
arguments.
– Colour options

For example: Colour symbol


plot(x,y,’m’) Blue b
Green g
Note: that Red r
m for Cyan c
magenta is
Magenta m
in single
quotes Yellow y
Black k
White w
2D Graphics – Symbol options
Marker symbol
For example: Point .
plot(x,y,'ms',x,z,'kx')
Circle o
Cross x
Plus sign +
Asterisk *
Square s
Diamond d
Triangle (down) v
Triangle(up) ^
Triangle(left) <
Triangle (right) >
Pentagram p
Hexagram h
2D Graphics – Line style options
The above tells plot to graph x
vs y with red markers/lines
using plus symbols for the
markers and a dotted line. Plot
is also to graph x vs z with
black markers/lines using
asterisks as symbols and a
dash-dot line.

For example:
plot(x,y,‘r+:',x,z,‘k*-.')

Linestyle symbol
Solid line -
Dotted line :
Dash-dot line -.
Dashed line --
2D Graphics – The plot function
• It is possible to add grid lines to a plot at the tick
marks by using the command ‘grid on’. Further,
the 2-D axes are usually enclosed by a box, this
box can be removed with the command ‘box off’,
for example:

x = linspace(0,2*pi,30);
y = sin(x);
z = cos(x);
plot(x,y,'r+:',x,z,'k*-.')
grid on;
box off;
Multiple Plots
• What happens if you type the following?

x = linspace(0,2*pi,30);
y = sin(x);
z = cos(x);
plot(x,y,'r+:')
plot(x,z,'k*-.')

Only the second plot is


shown!!

This is because MATLAB


Automatically clears the
screen every time the ‘plot’
function is used.
Multiple Plots
Using the ‘hold on’ command we force
MATLAB not clear the screen each time we
access the figure.

x = linspace(0,2*pi,30);
y = sin(x);
z = cos(x);
hold on;
plot(x,y,'r+:')
plot(x,z,'k*-.')
To return to MATLABS default,
we may type hold off.
2D Graphics – The plot function
This has only been an introduction to creating 2D
plots. It is all that is needed for this subject,
however MATLAB help provides excellent detailed
examples:
3D Plots – line plots
• The three dimensional equivalent to
the 2D ‘plot’ command is 'plot3'.
• The general format of the function
call is:
plot3(x1,y1,z1,S1,x2,y2,z2,S2,…)

• Where xn, yn and zn are arrays or


matrices and Sn are optional
character strings specifying colour,
marker symbol, and/or line style.
3D Plots – line plots
• Here is an example:

t = linspace(0,10*pi,200);

plot3(sin(t),cos(t),t)
title('Figure 6.3: Helix')
xlabel('sin(t)')
ylabel('cos(t)')
zlabel('t')
grid on

xlabel, ylabel and zlabel provide


labels for the x, y, and z axes on
the graph (as shown). The
functions xlabel and ylabel can
also be used for 2D plots.
3D Plots – line plots
• As with 2D plots, it is possible to have
more than one plot on the same
axes:
t = linspace(0,3*pi,200);

hold on
plot3(sin(t),cos(t),t,'g-')
plot3(sin(t),cos(t),t,'s')
plot3(sin(pi*t./2),cos(pi*t./2),t,'mo')
plot3(sin(pi*t./2),cos(pi*t./2),t,'b-')
title('Figure 6.3: Helix')
xlabel('x')
ylabel('y')
zlabel('t')
3D Plots – mesh plots
• It is also possible to create ‘mesh plots’,
such as the one shown below. These are
plots of three-dimensional function
surfaces.

• ‘mesh plots’ require two


commands to be used
in conjunction with each
other:
– meshgrid
– mesh
3D Plots – mesh plots
• Let’s first look at the command
meshgrid.
x = -3:1:3
y = -3:1:3
[X,Y] = meshgrid(x,y)
Z = X.^2 - Y.^2
First we have produced two arrays, x and y, these specify the range in
both the x and y directions.

x=
-3 -2 -1 0 1 2 3
y=
-3 -2 -1 0 1 2 3
>>

MATLAB cannot produce a 3D plot using these arrays,


instead we have to make two matrices using meshgrid.
3D Plots – mesh plots
[X,Y] = meshgrid(x,y)
Z = X.^2 - Y.^2

X= Z=
-3 -2 -1 0 1 2 3 0 -5 -8 -9 -8 -5 0
-3 -2 -1 0 1 2 3 5 0 -3 -4 -3 0 5
-3 -2 -1 0 1 2 3 8 3 0 -1 0 3 8
-3 -2 -1 0 1 2 3 9 4 1 0 1 4 9
-3 -2 -1 0 1 2 3 8 3 0 -1 0 3 8
-3 -2 -1 0 1 2 3 5 0 -3 -4 -3 0 5
-3 -2 -1 0 1 2 3 0 -5 -8 -9 -8 -5 0
Y= >>
-3 -3 -3 -3 -3 -3 -3
-2 -2 -2 -2 -2 -2 -2
-1 -1 -1 -1 -1 -1 -1 Meshgrid creates the matrices X
0 0 0 0 0 0 0 and Y. The matrix Z is made from
1 1 1 1 1 1 1 the second line.
2 2 2 2 2 2 2
3 3 3 3 3 3 3 A 3D plot can be made from the
matrices X, Y and Z.
3D Plots – mesh plots
• Second, we plot the function:
x = -3:0.5:3
y = -3:0.5:3
[X,Y] = meshgrid(x,y)
Z = X.^2 - Y.^2
mesh(X,Y,Z)

Here the function that we are


plotting is:

Note that the colour of the plot


varies to indicate the value of z.
Also note that the background grid
is shown by default.
3D Plots – surface plots
• A Surface plot looks like a mesh plot,
except that the spaces between the lines
are filled in. To generate a surface plot, we
use the surf command:

x = -3:0.5:3
y = -3:0.5:5
[X,Y] = meshgrid(x,y)
Z = X.^2 - Y.^2
surf(X,Y,Z)
3D Plots – surface plots
• You may not want to see the lines on your
contour, this may be achieved by using the
function ‘shading flat’

x = -3:0.5:3
y = -3:0.5:5
[X,Y] = meshgrid(x,y)
Z = X.^2 - Y.^2
surf(X,Y,Z)
shading flat
3D Plots – surface plots
• You may want to see a smooth surface
plot, this may be achieved by using the
function ‘shading interp’, note that the
lines are also removed.

x = -3:0.5:3
y = -3:0.5:3
[X,Y] = meshgrid(x,y)
Z = X.^2 - Y.^2
surf(X,Y,Z)
shading interp
3D Plots – contour plots
• Contour plots show lines of constant
elevation or height. In MATLAB, contour
plots are generated using the command
contour:
x= -3:0.5:3
y = -3:0.5:3
[X,Y] = meshgrid(x,y);
Z = X.^2 - Y.^2;
contour(X,Y,Z,50)

This is the same function as before.


Note that the colour of the contour
lines varies with the value of the z
component. Note that there are 50
contour lines here.
3D Plots – contour plots
• It is also possible to generate filled
contours using the function contourf.

x= -3:0.5:3
y = -3:0.5:3
[X,Y] = meshgrid(x,y);
Z = X.^2 - Y.^2;
contourf(X,Y,Z,20)

Note that there are 20 contour


regions shown. The function
contourf has exactly the same form
as the function contour.
3D Plots
• As with our examples of 2D plots, we have
only looked at a small component of
MATLAB’s possibility. You may find more
information using MATLAB help, 3-D
Visualization:
An Example

x = -5:0.2:5;
y = -5:0.2:5;

[X,Y] = meshgrid(x,y);

Z = (-3*Y)./(X.^2 + Y.^2 + 1)
mesh(X,Y,Z)

Here the function that we are plotting is:


An Example

x = -5:0.2:5;
y = -5:0.2:5;

[X,Y] = meshgrid(x,y);

Z = (-3*Y)./(X.^2 + Y.^2 + 1)
surf(X,Y,Z)
shading interp

Here the function that we are plotting is:


An Example
x = -5:0.2:5;
y = -5:0.2:5;

[X,Y] = meshgrid(x,y);

Z = (-3*Y)./(X.^2 + Y.^2 + 1)
contour(X,Y,Z,20)

Here the function that we are plotting is:


An Example
x = -5:0.2:5;
y = -5:0.2:5;

[X,Y] = meshgrid(x,y);

Z = (-3*Y)./(X.^2 + Y.^2 + 1)
contourf(X,Y,Z,40)

Here the function that we are plotting is:


Lecture Summary
• Today we focused on:
• MATLAB data files, saving and loading your data.
• 2D plotting techniques
• 3D plotting techniques
• SUBPLOT ??
You need to be proficient in all of these
techniques.
Next Lecture
Function files:
• Writing M-files which can easily be
used by others.
• Modular programming techniques
and why they are useful.

• Examples of programming using


Matlab.

You might also like