This week's lab continues our work with if/else
from Chapter 3 of the textbook.
It also introduces two new concepts:
try/catch
from Chapter 3while
loops from Chapter 4 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.)
Before starting this lab:
if/else
statement, on pages 91-113, and the material about try/catch on pp. 114-117. while
loop on pp. 147-153.fprintf
function on pages 41-43, in Chapter 2. 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.
There are two ways to copy the files for this week's lab. Read over both methods before deciding which one to use.
/www/htdocs/CIS/106/pconrad/06F/labs/lab06
Assuming that you are already in ~/cisc106/lab06
as your current working directory, the following command will copy all the files you need.
cp
, and right after the *
>> !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.
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.
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.
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 ...
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).
while (condition)
statement and the end
statement) is executed once, and then the condition is tested again.title
(which this time might actually be "done") end
—whichever end
matches the while
end
, but it is possible for there to be an if/elseif/else/end
inside the while
loop—in that case, MATLAB skips to the matching end
. This is not to "turn in"—it is only for practice.
gradeCalculator.m
to gradeCalculator2.m
. Use the Unix cp
command to do it, like this:
>> !cp gradeCalculator.m gradeCalculator2.m
>>
if/elseif/elseif/else
structure inside the while loop, just like you nested an if/elseif/elseif/else structure inside the for loop in lab05.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 |
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..
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.
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.
Using the file hurricane.m
file as a model, your job is to write an M-file called lab06.m
that
http://copland.udel.edu/~username/cisc106/lab06/filename.dat.wind.jpg
while
loop to accomplish the repetition) hour = hurricaneData(:,1); month = hurricaneData(:,2); day = hurricaneData(:,3); ...
and so on, then hour
, month
and day
are vectors, but hour(1)
is a scalar containing only the hour entry for the first row in the file, and day(1)
is a scalar containing only the day entry for the first row in the file.
.
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.
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!