E5 MATLAB Assignment 3

This exercise builds upon the previous one.  Upon completion of the exercise you should be able to use MATLAB to

As you go through this document, you should enter the commands at the MATLAB prompt, and try variations on the commands to make sure that you fully understand. 

Reading from text

You may want to refer to chapters 1,  2 and 5 of your text.


(§2.1 in your text - relevant textbook sections will be bold purple text)  You have learned to store and manipulate individual numbers (scalars) as well as lists of numbers (vectors).  The notion of a list of numbers can be extended to multiple dimensions, giving rise to "arrays" of numbers.  In E5 the most common type of array is a two dimensional array.  To create an array with 2 rows and 3 columns (called a two-by-three, or 2x3 array), array we enter one row at a time, separating the rows by a semicolon.

>> a = [1 4 5; 8 -1 6]
a =
     1     4     5
     8    -1     6

(Note: you can cut the code from this document and past it directly into MATLAB; MATLAB will ignore any prompt characters, ">>" that are pasted.) 

Sometimes it can be useful to enter the rows on separate lines

>> a = [1 4 5;
8 -1 6]
a =
     1     4     5
     8    -1     6

The size of an array can be determined

>> size(a)
ans =
     2     3

Note that when refering to arrays as n-by-m, this always means n rows my m columns.  So the array "a" is 2x3 or 2 rows by 3 columns.  The row dimension always comes first.  To access the element in the 1st row and 2nd column, use two "indices."  Note: "indices" is the plural of "index" (and "matrices" is the plural of "matrix").

>> a(1,2)
ans =

For the next several examples define a bigger array, a 3x6 array of random numbers using MATLAB's "rand" function.

>> myMat = rand(4,7) % Define a 4x7 array of random numbers
myMat =
0.7922 0.8491 0.7431 0.7060 0.0971 0.9502 0.7655
0.9595 0.9340 0.3922 0.0318 0.8235 0.0344 0.7952
0.6557 0.6787 0.6555 0.2769 0.6948 0.4387 0.1869
0.0357 0.7577 0.1712 0.0462 0.3171 0.3816 0.4898

To define a new matrix that consists of rows 2 to 4 and columns 3 to 6, use a range of numbers for the indices (note: make sure you understand this step)

>> newMat = myMat(2:4,3:6)
newMat =
    0.3922    0.0318    0.8235    0.0344
    0.6555    0.2769    0.6948    0.4387
    0.1712    0.0462    0.3171    0.3816

To pick rows 2 and 3, but all of the columns use a colon, ":", in the second index (the column index).

>> row2_3 = myMat (2:3,:)
row2_3 =
    0.9595    0.9340    0.3922    0.0318    0.8235    0.0344    0.7952
    0.6557    0.6787    0.6555    0.2769    0.6948    0.4387    0.1869

Picking all of row 4 results in a 1x4 array, which is the same as the vectors (technically "row vectors") that you worked with last week.

>> row4 = myMat(4,:)
row4 =
    0.0357    0.7577    0.1712    0.0462    0.3171    0.3816    0.4898

To pick the first column (rows=all, column=1), and a "column vector" results

>> col1 = myMat(:,1) 
col1 =
    0.7922 0.9595

Plotting and matrices

Consider the matrix created earlier.

>> newMat
newMat =
    0.3922    0.0318    0.8235    0.0344
    0.6555    0.2769    0.6948    0.4387
    0.1712    0.0462    0.3171    0.3816

If we would like plot the individual rows we can create a vector for the x values of the plot (1 through 4, since there are four values), and plot each of the three the rows individually.

>> x=1:4
x =
     1     2     3     4
>> plot(x,newMat(1,:), x,newMat(2,:), x,newMat(3,:))  % Plot multiple lines
>> legend('Row 1','Row 2','Row 3')

There is also a quicker way to do this.  It is described in the MATLAB help documentation.  It describes how you can use the "plot" command to plot all of the rows (or columns) of a matrix individually.  Note: This can also be found in the documentation browser (accessed through the "doc" command) by going through the documentation tree MATLAB→User's Guide→Graphics→Basic Plotting Commands→Line Plots of Matrix Data.

"If Y is a matrix, and x is a vector, plot(x,Y) successively plots the rows or columns of Y versus vector x, using different colors or line types for each. The row or column orientation varies depending on whether the number of elements in x matches the number of rows in Y or the number of columns. If Y is square, its columns are used."

In our case the vector "x" is of length 4, and the matrix "newMat" has four columns.  Using this description the three separate rows can be plotted with one short line (plus one line for legends).

>> plot(x,newMat)  % Plot multiple lines (using more compact code)
>> legend('Row 1','Row 2','Row 3')

This generates the same graph as the one shown above.

Importing Data

For the assignment this week you will need to import some data from an Excel spreadsheet and save it in MATLAB fomat.  Start by downloading the file "INTERVALDATA_6197101104_19081.xlsx" to your MATLAB directory.  The file  was provided by the electrical utility and it contains data, in half hour intervals, of the college's electric use (in a unit of kiloWatt-hours - a measure of energy.  You should see the file in the "Current Folder" pane of the MATLAB window (see below).

Right click on the file and choose "Import Data..." as shown above.

An "Import" window will open.  The columns in excel are enumerated alphabetically.  Column A is the college's account number, B is the meter number, C is the date, and rows D through AY are 48 half hour intervals throughout the day.  Scroll down to row 106 in the spreadsheet (the date in the third column will read "NAN" (or Not A Number), or show the date).  We want to import the data from the month of February, and this will consist of 29 rows and 48 columns. So, for the import range put in "D106:AY134" as shown below.


Hit the import button (the green arrow in the right of the "Import" pane).  This will create a variable called "untitled" that is 29x48  give it a more meaningful name and save it.

>> febElecUse = untitled;
>> save febElecUse

To recap: the file "febElecUse.mat" that was created has in it a variable called "febElecUse" that is a 29x48 matrix representing the college's electric use in February of 2012.  The columns represent 1/2 hour intervals starting at midnight of each day, and each row represents a day.  The first row represents February 29th.

Reversing elements in a vector

This may be a useful hint for completing the assignment

Hint: to create a vector and then reverse the elements you can use the following code (make sure you understand this):

>> x = 0:2:10		% Create a vector with even numbers from 0 to 10.
x =
     0     2     4     6     8    10
>> y = x(6:-1:1)	% Reverse the vector.
y =
    10     8     6     4     2     0

Recall that using the colon operator with three arguments, "x0:xinc:xf", creates a vector starting at x0, incrementing by xi and finishing at x0.  If there are only two arguments then the value xinc defaults to one.

To do

The task this week is similar to the previous one, however the code will be much shorter because you will use matrix structures instead of just vectors.  The task assumes you have created a file called "febElecUse.mat" as described in the previous section.

Your task is to

  1. Load the script "E5M3.m".
  2. The matrix "febElecUse" has the last day of the month as the first row, and the first day of the month as the last row.  Reverse that so the first row is the first day.  You may want to use the hint above.
  3. Create a graph showing energy usage for the first seven days of February. 
  4. Make sure your code has sufficient comments.  Much of your grade will depend on the clarity of these comments.  In particular, add a section at the top that describes what the code does.  Also add comments, as appropriate, throughout the file. 
  5. When you get your code working, publish it to a Word document.

Hints/techniques to consider while debugging your script:

To turn in

(via moodle)

Grading is as follows:

Arrays with more than two dimensions

This topic may be skipped - read only if you want to.

You can define matrices with more than 2 dimensions.  For example, a 2x5x4 matrix of random numbers.

>> mat3D = rand(2,5,4)
mat3D(:,:,1) =
    0.6020    0.6541    0.7482    0.0838    0.9133
    0.2630    0.6892    0.4505    0.2290    0.1524
mat3D(:,:,2) =
    0.8258    0.9961    0.4427    0.9619    0.7749
    0.5383    0.0782    0.1067    0.0046    0.8173
mat3D(:,:,3) =
    0.8687    0.3998    0.8001    0.9106    0.2638
    0.0844    0.2599    0.4314    0.1818    0.1455
mat3D(:,:,4) =
    0.1361    0.5797    0.1450    0.6221    0.5132
    0.8693    0.5499    0.8530    0.3510    0.4018

Call the third dimension the "level."  The element in the 2nd row, 4th column and 3rd level is 0.1818.  This is depicted below followed by MATLAB code.

>> mat3D(2,4,3)
ans =