Last week, in lab03, we started with a simple data file of numbers—in this case, data about Hurricane Katrina (from August of 2005)—and produced two graphs
In the process, we also learned a few things about slicing up Matrices in MATLAB, i.e. selecting out particular rows and columns.
This week, in lab04, we'll add two more graphs:
and do a bit more practice with selecting out rows and columns from matrices.
We'll also learn a little bit of HTML so that we can incorporate all four graphs into a simple web page.
Before starting this lab, you should already be comfortable with the following skills from lab01, lab02 and lab03.
( As you can see, we've already learned quite a bit!)
pwd
, mkdir
, and cd
commands to manage directories in MATLAB
By the time you complete this lab, you should be able to:
... but you can do it on a PC if you want to.
Here's why doing this week's lab on the Sun Ray will be easier, and how to work around it so that you can do it at home.
You can review the instructions from lab01 for starting up MATLAB on the SunRays if you are not sure how to do this.
Create a new subdirectory ~/cisc106/lab04, 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.
Assuming that:
then, the following command can be used to copy that file into your current directory.
>> !cp ../lab03/katrina.dat . >>
Some notes (some of this is review from lab03, step 1d):
!
at the beginning of the command. This signifies to MATLAB that what you are typing is actually not a MATLAB command, but instead a command to the Unix operating system.cp
command is the Unix command to copy a file.copyfile
in place of !cp
copyfile
is a MATLAB command, and therefore should work on all different types of systems!cp
command instead.!cp
is also less typing than copyfile
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.
So you could skip it if you wanted to, and neither your instructor nor your TA would know.
Well, actually that's not entirely true. We'd know if,
So, don't skip this step.
Enter the following in the MATLAB command window:
>> a = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16] a = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 >>
Note that we can actually enter the same matrix faster this way:
>> a = [1:4; 5:8; 9:12; 13:16] a = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 >>
Now try typing in the following assignment statement:: b = a(2:end,:)
as shown below. See if you get the same output.
>> b = a(2:end,:) b = 5 6 7 8 9 10 11 12 13 14 15 16>>
Notice that in effect, what happens is that the variable b
is assigned a new matrix, which is the same as the a
matrix but with its top row chopped off. This is because the expression 2:end
takes all the rows from 2 to the last row, and the expression :
takes all columns.
Now, try each of the following assignments. In each case, observe what the result is for b. Better yet, try to predict what b will be before you type in each statement.
b = a(:,2:end)
b = a(1:end-1,:)
b = a(1:end-1,1:end-1)
b = a(:,end)
b = a(end,end)
You'll need this skill of "slicing off" one row of a matrix to complete part 3 of this week's lab.
Now, once again load the katrina.dat
file into a matrix called katrina
, and set up the format so that the numbers don't show up in scientific notation.
We want to produce two more plots:
Open lab04a.m in the MATLAB editor window.
Start the lab04a.m file with a command that loads the katrina.dat file into the workspace.
Note that the file starts with an observation taken at 18 hours GMT on 08/23/2005. We are going to slice off this top row, and start our graphs with the row that corresponds to the observation at midnight on 08/24/2005. This will simplify our calculation for elapsed time.
So, after the command that loads the katrina.dat file, issue a MATLAB command to slice off the top row, and store the result back into a new matrix called katrina2
. Put this command in your .m file as well, and test whether it works.
Next, construct column vectors for hours, days, wind and pressure, corresponding to the correct columns of katrina2, using the techniques we covered in step3a of lab03.
Then, construct a vector for elapsedTime (measured in hours), which measures the elapsed time in hours from the starting point of midnight on 08/24/2006. The vector should start with the entries 0 6 12 18 24, etc and have exactly the same number of entries as there are rows in the new katrina2 matrix.
Think in terms of coming up with a formula for "elapsedTime" in terms of the corresponding entries in the hours and days vectors.
Then, put the commands into your M-file lab04a.m to make a plot with wind vs elapsedTime (with time on the x axis, wind on the y axis). The M-file should function even from a clear workspace---that is, if you type clear
;, followed by the name of the M-file (lab04a
;) you should get a correct result. Put a reasonable legend and title on your plot. Include your full name in the title.
Save the result of your plot in a file called lab04a.jpg
Now create an M-file lab04b.m to make a similar plot with pressure vs elapsedTime (with time on the x axis, pressure on the y axis). All the same criteria as in Step 3d apply.
Save the result of your plot in a file called lab04b.jpg
At the MATLAB command prompt (working from a SunRay, or directly on strauss via SSH) type the following commands as shown.
>> !mkdir -p ~/public_html/cisc106/lab04
>> !chmod a+x ~ >> !chmod -R 755 ~/public_html >>
Here is what each of these commands does
!mkdir -p ~/public_html/cisc106/lab04
command creates an entire path of directories that will correspond to the web site http://copland.udel.edu/~username/cisc106/lab04, where
!chmod a+x ~
is necessary to make sure that the web server can "pass through" your home directory to find your public_html directory. Otherwise, you might get a "403 Forbidden" error message when you try to bring up your files on the web.!chmod -R 755 ~/public_html
command makes everything in your public_html folder, and every subdirectory under it, available on the web.
-R
stands for recursive, and it means "don't just do the command to this one directory---do it to also to every directory inside this directory, and every directory inside all those directories, and so on, and so on." All the files in the directories get affected also.755
part is an octal number specifying a permission mode. When this number is applied to a file (or directory), it signifies that only you can make changes to the file (or directory), but other people are allowed to read the file (or browse through the directory.)~/public_html
part refers to the root directory for all your web content. To see if these commands work, try opening a web browser to the page http://copland.udel.edu/~username/cisc106/lab04, except change username to your actual username. Don't forget the tilde (i.e. the "squiggle", the ~ symbol!)
You should see an empty web page, something like this:
\
If, instead, you get an error such as "403 Forbidden", then try the commands again, and/or ask your TA for assistance.
As we copy files into the directory ~/public_html/cisc106/lab04, these files will show up in this directory. To make them readable, we may need to do the following command again, each time we add files to this directory:
Note the important distinction between
The following commands--if done in MATLAB on strauss---will copy the files from last week's lab into your web directory.
strauss.udel.edu
strauss.udel.edu
is where MATLAB is actually running. >> !cp ~/cisc106/lab03/*.jpg ~/public_html/cisc106/lab04 >>
Explanation:
!cp
part should be familiar by now *.jpg
part means "all files that end with .jpg". This will copy both lab03a.jpg
and lab03b.jpg
with one command.Now, type the following command to make these newly copied files readable on the web:
>> !chmod -R 755 ~/public_html >>
Then, go to the web site http://copland.udel.edu/~username/cisc106/lab04, again (except change username to your actual username) and you should be able to click on both of your jpeg files.
Temporarily, change your working directory to ~/public_html/cisc106/lab04. You do this with the following command:
>> cd ~/public_html/cisc106/lab04 >>
Then, in the MATLAB command window, type edit katrina.html
>> edit katrina.html >>
In the edit window that comes up, enter the following text. In the places that the name Daisy Duck appears, please change the name to your own name, and fill in your section number, etc.
There are also several places where there are @@@ symbols—this signifies that you should fill in some comment of your own, rather than what I have written here. You don't have to do this for the first draft... for now, you can type it in just as it is here.
<html> <!-- katrina.html Daisy Duck for CISC106, Fall 2006, Section 010 --> <head> <title>Hurricane Katrina plots, by Daisy Duck for CISC106 Section 010</title> </head> <body> <h1>Katrina Plots</h1> <h2>By Daisy Duck for CISC106, Section 010 </h2> <h3>Katrina's Path</h3> <p> This plot shows Katrina's position from 1800 GMT on 08/23/2005
through 0600 hours GMT on 08/31/2005 </p> <img src="lab03a.jpg" alt="diagram of Katrina's position"/>
<h3>Wind speed vs. Pressure</h3> <p> This plot shows wind speed in mph on the x axis, and barometric pressure in millibars on the y axis. From this plot we can observe that @@@ blah blah blah blah blah blah blah blah continue over as many lines as you need to say something intelligent about this plot. When you are done remove all this nonsense and the at signs at the beginning and the end @@@ </p> <img src="lab03b.jpg" alt="plot of wind speed vs. barometric pressure"/> <h3>Plot of wind speed vs. elapsed time</h3>
<p> @@@ A description of this plot will go here @@@ </p> <img src="lab04a.jpg" alt="plot of wind speed vs. time"/> <h3>Plot of barometric pressure vs. elapsed time</h3>
<p> @@@ A description of this plot will go here @@@ </p> <img src="lab04b.jpg" alt="plot of pressure vs. time"/> </body> </html>
When you have typed all this in, save it and then once again do the command to make things readable on the web:
>> !chmod -R 755 ~/public_html >>
Then, once again visit the web address http://copland.udel.edu/~username/cisc106/lab04 and this time click on the file katrina.html.
You should see a web page that contains your four plots, plus your commentary about them.
Now, if you left the @@@ placeholder text in on your first go around, go back and fill it in with some more intelligent comments. I'm not looking for profound metrological insights—I just want to see that you actually took a few seconds to look at the graph and figure out what it means.
Also, hopefully this text is in your own words, and not that of any of your classmates—it would be truly remarkable if two people chose to describe their graphs in exactly the same way.
Probably because you didn't copy them yet! Look back at the command we used to copy the jpg files from last week's lab03 directory into your web directory. The files have to be in the same directory with the katrina.html
file for them to show up on that page.
Issue a command to copy those files into the proper directory. Remember to also do the chmod
command that makes the newly copied files readable to the web server.
public_html
directory so that you can reference them. But the specific way we coded the HTML file, they do have to be in the same directory. Before uploading to WebCT, be sure that your web page at
http://copland.udel.edu/~username/cisc106/lab04/katrina.html
is looking spiffy. The TA will take your submission on WebCT as a signal that your web page is ALSO ready for grading, so don't submit until that part is also done.
Once it is, you should upload and submit your two new M-files, your two new JPEG files, and your katrina.html file on WebCT, and submit.
That's it for lab04!