lab06, CISC106, Fall 2006

Overview

This week's lab continues our work with if/else from Chapter 3 of the textbook.

It also introduces two new concepts:

We also see how we can use string variables to allow the filename loaded by a MATLAB M-file to be entered from the keyboard instead of hard-coded into the M-file. That way, we can reuse the same M-file to process several different input files.

Specifically, we'll take a look at an M-file that can process data for any hurricane—not just the data for Hurricane Katrina—as long as the data is in a particular format. You'll then take this M-file, and modify it so that it can do all the things we did with the Hurricane Katrina data, but for any hurricane. (I've supplied you with data for the first five named storms of the Atlantic 2006 Hurricane Season: Alberto, Beryl, Chris, Debbie, Ernesto, and Florence.)

Prerequisites

Before starting this lab:

Step-by-Step Instructions

Step 1: Preparing your lab06 directory, and copying files you will need

Step 1a: Create a new subdirectory for lab06

Create a new subdirectory ~/cisc106/lab06, and make that your working directory.

If you are not sure how to do this, then review the instructions from lab03, steps 1a through 1d.

Step 1b: Copy the files needed for this week's lab

There are two ways to copy the files for this week's lab. Read over both methods before deciding which one to use.

  1. Download them from the web page http://www.udel.edu/CIS/106/pconrad/06F/labs/lab06
  2. If you are working on strauss, copy them directly from the directory /www/htdocs/CIS/106/pconrad/06F/labs/lab06
Copying all the files at once (if you are on strauss)

Assuming that you are already in ~/cisc106/lab06 as your current working directory, the following command will copy all the files you need.

>> !cp /www/htdocs/CIS/106/pconrad/06F/labs/lab06/* .
>>

There are additional notes about this command at lab04, step 1b if you need to review how this command works.

Step 2: Learning about the while loop

First: there is nothing to turn in for Step 2

The work you do in step 2 of this lab is only for practice and learning. There is nothing to turn in from this step of the lab.

But don't skip this step.

You'll look pretty foolish if you end up asking questions about stuff you should have learned by doing this step.

It would also be a shame to miss some easy questions on the exam about stuff you could have learned by doing this step.

So, don't skip it.

Step 2a: Working with the while loop: gradeCalculator.m

The while loop in MATLAB (and most other programming languages) allows you to do something over and over again, as long as some condition remains true.

A very common use of the while loop is to repeatedly do some command as long as this statement is true:

That is, the program asks the user for something---e.g. a filename, or a piece of input---and if the user wants to continue, she/he types in the input---otherwise, she/he types in something like "quit" or "done".

Consider the following MATLAB session, as an example:

>> gradeCalculator
Welcome to the grade averager.  If you
input a sequence of grades, I can compute the average.

I'll ask for grades one at a time.
  * First I'll ask you for a title for each grade (e.g. "hwk1")
  * Then I'll ask for the score out of 100 points
  * Enter "done" for the title when you are finished

Enter a title for this grade (or "done" when finished): hwk1
Enter a grade out of 100 points for hwk1: 90
Enter a title for this grade (or "done" when finished): hwk2
Enter a grade out of 100 points for hwk2: 70
Enter a title for this grade (or "done" when finished): hwk3
Enter a grade out of 100 points for hwk3: 50
Enter a title for this grade (or "done" when finished): done
The average of your 3 grades is  70.0
>>

This program is called gradeCalculator.m and it is one of the programs you copied from the lab06 directory.

I highly recommend, before you go any further, that you try executing this M-file yourself in MATLAB. You'll learn more by doing that, than by reading about the program.

Now that you've tried running it, look at how the program works. Here is an excerpt from the M-file, showing the while loop part, and providing an explanation:

title = input('Enter a title for this grade (or "done" when finished): ','s');
while (title ~= 'done')
     % ask user for a grade

     prompt = ['Enter a grade out of 100 points for ' title ': '];
     thisGrade = input(prompt);

     count = count + 1;
     grades(count)  =  thisGrade;

     title = ...
        input('Enter a title for this grade (or "done" when finished): ','s');
end

% The program now continues with steps that calculate the average
...
Explanation of the while loop

Before the while loop starts, the code asks for a title for the grade (or for the user to input the word "done").

Then. the while loop condition checks that the title is not equal to the word "done" (the ~= operator in MATLAB).

Now, try this challenge for practice

This is not to "turn in"—it is only for practice.

  1. Copy the file gradeCalculator.m to gradeCalculator2.m. Use the Unix cp command to do it, like this:
    >> !cp gradeCalculator.m gradeCalculator2.m
    >>
  2. Edit the file gradeCalculator2.m so that in addition to calculating a grade average, it also outputs letters grades such as A, B, C, D and F—or if you are really ambitious, plus minus grades as well—as you did in step2 of lab05. The grade table is repeated below for your convenience.
  3. Once you've coded the solution, test it out to make sure it works.
grade >= 93 A 73<= grade < 77 C
90 <= grade < 93 A- 70<= grade < 73 C-
87 <= grade < 90 B+ 67 <= grade < 70 D+
83<= grade < 87 B 63<= grade < 67 D
80<= grade < 83 B- 60<= grade < 63 D-
77 <= grade < 80 C+ grade < 60 F

Step 2b: Looking at the hurricane.m file

Now, notice that there is a file in your lab06 directory called hurricane.m along with several .dat files for various hurricanes.

These .dat files have a couple of extra columns—even the katrina.dat file has these extra columns, making it different from the previous version of katrina.dat.

The extra columns are columns 2 and 4, which contain the month and the year..

Try running the hurricane.m M-file

Run the file.

It will ask you for a filename. Enter one of the .dat files (e.g. katrina.dat). It will think for a minute, then give you a URL where you can go look at a web page. See what is on that web page.

Then, it will ask you for another filename. Try one of the other files. Then, try the URL it gives you.

Then, try a filename that does not even exist (e.g. foobar.dat). See what happens.

Finally, type in quit and see what happens.

Then, look at the M-file and try to understand how it works.

If you have questions, Your TA and I will freely explain how hurricane.m works

However, we'll be a lot less free with the explanation of how to do Step 3! That is for you to figure out on your own. So ask questions at step 2 if you don't understand. Once you do, you are ready to tackle a problem on your own, in Step 3.

Step 3: Create an M-file that produces the wind vs. hours elapsed graph for any hurricane data file

Using the file hurricane.m file as a model, your job is to write an M-file called lab06.m that

  1. Prompts the user for the name of a hurricane data file (or the word"quit")
  2. If the user types quit, stop the program
  3. Otherwise, try to load the information from the filename supplied by the user
  4. Plot a table of wind speed vs. elapsed time, with the starting point being the first observation in the file
  5. Save the file to a JPEG file readable on the web at http://copland.udel.edu/~username/cisc106/lab06/filename.dat.wind.jpg
  6. Then repeat everything above from step 1 (use a while loop to accomplish the repetition)
Some hints

.

Step 4: Create a diary file lab06.txt to show your work.

Once your lab06.m file works, create a diary file called lab06.txt in which you type out your lab06.m file, and then test it to show that it works.

You only need to run your program once, but you should run it with at least three different input files, and then type the name of at least one non-existing filename before you finally type "quit".

Be sure that the URLs that appear in your diary file are actually files on the web that your TA can check.

Step 5: Submit two files on WebCT.

The two files you need to submit are lab06.m and lab06.txt. Upload these to WebCT and submit.

Your TA will also check the URLs that appear in your lab06.txt file.

That's it for lab06!


Grading

  1. lab06.m correctness (25 pts)
  2. lab06.m programming style (25 pts)
  3. lab06.txt (25 pts)
  4. http://copland.udel.edu/~username/lab06 should contain all three plots referred to in the diary file lab06.txt (15 pts)
  5. Generally submitting everything according to instructions: (10 pts).

End of lab06 for CISC106, Fall 2006 (100 pts)