This week's lab focuses on if/else
from Chapter 3 of the textbook, and for
loops from Chapter 4 of the textbook.
Before starting this lab:
if/else
statement, on pages 91-113. for
loop on pp. 153-163.fprintf
function on pages 41-43, in Chapter 2. Create a new subdirectory ~/cisc106/lab05, 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/lab05
Assuming that you are already in ~/cisc106/lab05
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/lab05/* .
>>
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 if/else is used to make a decision in MATLAB. It allows you to send MATLAB to choose between executing one set of command (the if
part), and another set (the else
part).
if/elseif/else
allows one of several different choices. You can have as many elseif
s as you need, but only one else
at the very end. if
with no else
at all.In your lab05 directory, you should find (at least) three files that you copied back in step 1b (there may be others too):
Use the type
command or the edit
command to look at the contents of these three files.
emacs
at the Unix prompt, instead of edit
at the MATLAB prompt. Also, run them and experiment with them, typing in different values until you are comfortable with how an if/else works.
type
filename.m
ifElseExample2.m
, enter type
ifElseExample2.m
at the MATLAB >>
prompt. edit filename.m
edit
command only works in the graphics interface to MATLABletterGrade.m
, you just type letterGrade
at the MATLAB >>
prompt. This is not to "turn in"—it is only for practice.
letterGrade.m
to letterGrade2.m
. Use the Unix cp
command to do it, like this:
>> !cp letterGrade.m letterGrade2.m
>>
letterGrade.m
, the first filename, is the source file (i.e where you are copying from).letterGrade2.m
(the second filename) which is the destination file (i.e. where you are copying to). cp
command is a Unix command, you have to put an exclamation mark (!
)in front of it when you type it at the MATLAB >>
prompt. copyfile
instead of !cp
, or just copy the file through the regular Windows or Mac desktop(73 <= grade < 77)
is perfectly reasonable in a MATH class, but is it NOT acceptable in MATLAB! if/elseif/elseif/else
structure pretty much the same as it already is, and just add in more criteriagrade >= 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 lab05 directory called grades.dat
. This is a file of grades (made up, not real ones) that you can load into a MATLAB matrix called grades
(actually, a vector)
Now try several commands:
grades
(without a semicolon) at the MATLAB prompt, to list out the contents of the grades
vector. length(grades)
at the MATLAB prompt, and see what you get.1:length(grades)
, and see what the result is. Now, use the type
or edit
command to look at the contents of listGrades.m
. This M-file will load the contents of grades.dat
, and list the contents one grade at a time, showing both the grade, and the corresponding letter grade. Notice how the M-file uses a for
loop and if/else
to accomplish its task.
Also notice the use of the fprintf
MATLAB function for formatted output. You can read about fprintf
on pages 41-43 of your textbook.
Experiment with this file. Try modifying it to see if you can make it do something different. Work with it until you are comfortable that you understand how it works, because this week's lab is essentially going to be just like the listGrades.m
file, except you'll be working with the katrina.dat
file again, and classifying hurricane strength instead of grades.
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 listGrades.m
as a model, your job is to write an M-file called lab05.m
that
katrina.dat
(which should be in your lab05
directory, copied in with all the other files),hour
, day
, and wind
At 18 hours GMT on 08/23/2005, Katrina was a Tropical Depression At 0 hours GMT on 08/24/2005, Katrina was a Tropical Depression At 6 hours GMT on 08/24/2005, Katrina was a Tropical Depression At 12 hours GMT on 08/24/2005, Katrina was a Tropical Storm At 18 hours GMT on 08/24/2005, Katrina was a Tropical Storm At 0 hours GMT on 08/25/2005, Katrina was a Tropical Storm At 6 hours GMT on 08/25/2005, Katrina was a Tropical Storm At 12 hours GMT on 08/25/2005, Katrina was a Tropical Storm At 18 hours GMT on 08/25/2005, Katrina was a Tropical Storm At 0 hours GMT on 08/26/2005, Katrina was a Cat 1 Hurricane At 6 hours GMT on 08/26/2005, Katrina was a Cat 1 Hurricane At 12 hours GMT on 08/26/2005, Katrina was a Cat 1 Hurricane At 18 hours GMT on 08/26/2005, Katrina was a Cat 2 Hurricane etc...
Use the table at Wikipedia article about the Saffir-Simpson_Hurricane_Scale to come up with your scale for the various categories of storm.
You can use the Weather Underground's Hurricane Katrina Archive to check your results.
Some hints:
hour
, day
and wind
vectors are all the same length, it doesn't matter which one you use to control the ending value for the for loop index. That is, for k = 1:length(wind)
and for k=1:length(day)
both will result in exactly the same set of values for k
. Create a diary file called lab05.txt
in which you type out your lab05.m
file, and then test it to show that it works.
This time, one run is sufficient to test your program since all of the program input comes from the katrina.dat
data file.
The two files you need to submit are lab05.m
and lab05.txt.
Upload these to WebCT and submit.
That's it for lab05!