CS 150 - Assignment 10 - Matlab
Due: Wednesday 5/10 at the beginning of class
For our final lab, we're going to be adding some additional
functionality to our weather program from Lab 8. The goal of Lab 8
was to write a program that could collect weather data over time.
Once you have this data, the next step is to analyze it! This lab
explores this data analysis using Matlab.
You may (and are encourage to) work with a partner on this lab.
Saving your work
We recommend storing your files on our department server "basin" for this lab.
instructions for connecting to basin,
which are also linked from the main course page.
Follow the instructions for Windows users to map a network drive from
your virtual Matlab session. Unfortunately, you'll have to do this
again each time you log on. Once you have a network drive, say Z:, you can access
it from Matlab by clicking on the little arrow to the left of the drive letter (C:)
above the folder pane.
Just to make sure everyone is clear, here is a quick review of
defining functions in Matlab:
- To define a function use the following syntax:
function return_var = function_name(parameters)
- function is a keyword (like def), indicating
that we are defining a function
- return_var = indicates that we will be returning a
value from this function. When the function finishes executing,
whatever value is in this variable (which you can name whatever you
want) will be returned by the function. If you do not need to return
any value, you should leave this portion off.
- You must put the function in a file with the same name as the
function with a .m after it. For example, if you're writing a
function called plot_weather, then it needs to go in a file
- This means that generally speaking we will define one function
per file (you can define others, however, they will not be
visible/callable outside of that file).
- You should include a "docstring" for each function which
is a block of comments immediately following the function header.
- The function... line should be the first line in the
file. Do NOT put any comments/docstring above the function
(only for scripts) since this will mess up the help
functionality for the function.
- You do not run a function file like you do in Spyder
(i.e. by pressing the run button). As long as the .m file is in your
current working directory, you can just call the function normally.
Reading and writing from files in Matlab can be done easily if the
data is represented in the file in a matrix-like format. I have
posted two -matrix files that can be read easily using
dlmread in Matlab here:
Download the datafiles
Each line in the -matrix file has 24 temperature
readings separated by a space representing hourly temperature readings
for one day. Download both of these files into your working folder
for this lab. Make sure you understand what the data in the files
Visualizing the data
In Matlab, write a function called plot_weather that takes a
matrix of weather data as a parameter and then plots the average,
maximum and minimum temperature for each day in the data. Your plot
should include appropriate labels for the x and y axis and should also
include a legend. The input matrix will be in the same format as the
data file, with each row having 24 entries and each row representing a
For example, to generate a plot of the test data you would type:
and the results should look something like:
>> test_data = dlmread('test-matrix.txt');
A few hints:
When you've got it working, try plotting the weather data from
Wisconsin (e.g. wisc-matrix.txt). The data should look much
more interesting since it's real data! Can you figure out around what
time the data set starts and ends?
- See help legend for more information about adding a legend.
- Most of the built-in functions in Matlab summarize data along the columns, so it may be helpful to transpose the data matrix (denoted with a single quote).
Save a copy of the plot of the Wisconsin data and submit it along
with your code. To save the plot, click on the "Save" button (it
looks like a disk) in the plot window. Under file type, select
Analyzing the data
Plotting the data allows us to analyze some aspects of the data, but
there are many interesting additional questions that we could also
ask. Write a Matlab function called weather_stats that takes
a matrix of weather data as a parameter and displays the following
information about the weather data:
In writing this function:
- The number of days in the data set.
- The overall average temperature.
- The coldest temperature in the data set.
- The hottest temperature in the data set.
- The average daily temperature fluctuation. The daily temperature
fluctuation is the difference between the hottest temperature and the
coldest temperature. The average daily temperature fluctuation is the
average of this fluctuation over the days.
- Number of days where the temperature was above 80 degrees.
- Number of days where the temperature was below 32 degrees.
- Number of days where it didn't get above freezing (i.e. 32 degrees).
- Average temperature on those days when the temperature got below 32 degrees.
- One additional interesting fact about the data of your choosing.
For example, the output from this function on the data from the test file would be:
- For at least one of these calculations you must write
another external function (i.e. in its own file) that returns a value
and that you use within your weather_stats function.
- You should also print labels for each data item so it's clear
what the data represents.
- You should try very hard to avoid for loops. Most of
these statistics can be written straightforwardly using matrix
operations with just one or two statements in Matlab (in fact, all of
them can be written without for loops, though one or two are
trickier). You will not receive full credit if you use for
Days in data set:
Average temperature fluctuation:
Number of days above 80:
Number of days below 32
Number of days were it didn't get above freezing:
Average temperature when temps got below 32:
A few hints:
- As with the previous function, it may be helpful to transpose the
data matrix (denoted with a single quote).
- Test each of these statistics one at a time.
- If you get stuck, try to work out a small example by hand.
- Sometimes it can be helpful to write the statistic using a
for loop and then once you have it working, try to figure
out a way to write it without the loop.
You may earn up to 2 extra points on this assignment. Below
are some ideas, but you may incorporate your own if you'd like. Make
sure to document your extra point additions in comments at the top of
- Write an additional statistic regarding the weather data.
- Plot additional weather statistics over the days. You can either
plot them on the same plot, or write another function to plot
- Write all of your Matlab code without using any for loops.
- Add your own...
When you're done
Make sure that your program is properly commented. For Matlab this means:
In addition, make sure that you've used good style. For Matlab
this includes avoiding using for loops.
- You should have a docstring comment for each function.
- You should have comments after the function docstring stating your name, course (including section number), assignment number and the date.
What to submit
To submit multiple files,
put all your files inside a new directory and then create a .zip
file from this directory. On the Macs, right-click on the directory
and select "Compress...". If you're working on Windows, right-click
on the file and select "Send to" then select "Compressed (zipped)
Folder" (or if you don't have this option, use Winzip). You will then see a file
with a .zip extension be created.
- All of your .m files (you should have at least 3)
- The .png plot file saved from plotting the Wisconsin data
Submit this .zip file digitally at the usual location.
generates appropriate plot 4
legend and labels 1
avg. temp 1
coldest temp 1
hottest temp 1
avg. temp fluctuation 1
days above 80 and below 32 2
days not above freezing 2
average temp when below 32 2
extra function 1
Comments, style 5
Extra points 2
total 23 + 2