Assignment 5

Due: Friday, April 6, by 5:00pm

You can turn in your assignment up until 5:00pm on 4/6/18. You should hand in both a hardcopy and electronic copy of your solutions. Your hardcopy submission should include printouts of five code files: printStudentInfo.m, mostFacebookFriends.m, getFavoriteSong.m, testStudentInfo.m and visualize.m. To save paper, you can cut and paste all of your code files into one file, but your electronic submission should contain the three separate files. Your electronic submission is described in the section How to turn in this assignment.

Reading

The following material from the fifth or sixth edition of the text is especially useful to review for this assignment: pages 187-188, 192-198, and 221-231. You should also review notes and examples from Lectures #8, 9, 11, and 12, and Labs #6 and 7.

Getting Started: Download assign5_problems

Use Cyberduck to connect to the CS server and download a copy of the assign5_problems folder onto your Desktop. This folder contains two data files named students.mat and rising_data.mat and one code file named test_visualize.m for the problems in this assignment.

Uploading your completed work

When you have completed all of the work for this assignment, your assign5_problems folder should include five code files, printStudentInfo.m, mostFacebookFriends.m, getFavoriteSong.m, testStudentInfo.m, and visualize.m. Use Cyberduck to connect to the CS file server and navigate to your cs112/drop/assign05 folder. Drag your assign5_problems folder to this drop folder. More details about this process can be found on the webpage on Managing Assignment Work.

Problem 1: Facebook Friends and Favorite Songs

In this problem, you are given data about the favorite songs and number of Facebook friends for a group of students, and will create a MATLAB program to perform the tasks of printing all the information about the group of students, accessing the favorite songs of particular students, and determining who has the most Facebook friends. The assign5_problems folder contains a file named students.mat that you can load into the MATLAB workspace. This file contains a single variable named students that is assigned to a vector of 10 elements. Each element of the vector is a structure with three fields that store the name, favorite song, and number of Facebook friends, for a particular student. The three fields of each structure are called name, song, and FBfriends.

Task 1: Print all the student information

Write a function named printStudentInfo that has a single input that is a vector of structures containing student information, as described above. This function should print out all the information stored in the vector, with each student's information printed in a nice format such as the following:

>> printStudentInfo(students)
andrea's favorite song is "Dark Horse" and she has 42 Facebook friends
kiara's favorite song is "Team" and she has 61 Facebook friends
jean's favorite song is "Pompeii" and she has 23 Facebook friends
neha's favorite song is "Happy" and she has 51 Facebook friends
...

Task 2: Who has the most Facebook friends?

Write a function named mostFacebookFriends that has a single input that is a vector of structures containing student information. This function should return two values, the name of the student with the most Facebook friends and the number of Facebook friends for this student.

Task 3: What is their favorite song?

Write a function named getFavoriteSong that has a two inputs, a vector of structures containing student information and the name of a student (a string). This function should return a string that is the name of the student's favorite song. If the name is not found in the vector, this function should return an empty string. You can assume that the input name is written with all lowercase letters, similar to the names in the students vector, and that all the names are unique. Use a while statement to find the location in the input vector that contains the input name. This loop should stop when the name is found. The strcmp function can be used to compare two strings.

Task 4: Putting it all together

Write a script named testStudentInfo.m that contains code to test all of your functions. In particular, this script should do the following:

  1. load the students.mat file
  2. call printStudentInfo to print out all the student information
  3. call the mostFacebookFriends function and print the values that are returned in a nice format, for example:
    alejandra has the most Facebook friends, with 142
    
  4. call the getFavoriteSong function within a loop that keeps asking the user to enter a name, and prints the name and student's favorite song, until the user enters something like x that is an invalid name that can stop the loop. A while statement should be used to implement this loop. If the user types a name that is not contained in the vector, a message should be printed that indicates this. The following is a sample printout for this part of the script:
    Enter the names of students whose favorite song you'd like to know
    Enter x when you'd like to stop
     
    Enter a name: tanya
    tanya's favorite song is "Counting Stars"
    Enter a name: jasmine
    jasmine's favorite song is "Timber"
    Enter a name: claire
    claire was not found
    Enter a name: yue
    yue's favorite song is "Pompeii"
    Enter a name: x
    Goodbye!
    

Problem 2: Rising Data

This problem was inspired by a visualization of rising global temperatures at the Bloomberg Company website. You will write a function to create your own visualization of rising data of this sort, and apply this function to data on rising monthly temperatures in the Boston area over the years from 1880 to 2014, and rising daily Dow Jones Industrial Averages over 51 weeks of 2014. An advantage of defining a function to create this visualization, rather than a script, is that a function can easily be applied to different data sources that are provided through an input to the function.

In the assign5_problems folder, there is a data file named rising_data.mat that contains two variables, temps_data and djia_data. The variable temps_data is assigned to a 135 x 12 matrix, where each row corresponds to a different year (1880 to 2014) and each column corresponds to a different month (January through December). The values stored in the matrix are the average monthly temperatures in degrees Celcius. The variable djia_data is assigned to a 51 x 5 matrix, where each row corresponds to a different week during the year 2014 and each column corresponds to a different weekday (Monday through Friday). The values in this matrix are the closing Dow Jones Industrial Averages for each day. The assign5_problems file also contains a script named test_visualize that loads the data files and calls the visualize function (that you will write) for each of these two data sources. Your visualize function should have five inputs:

  1. a 2D matrix of data where the rows and columns represent different time frames (e.g. years x months or weeks x days)
  2. a string to display as the label of the x axis of the data plot
  3. a string to display as the label of the y axis of the data plot
  4. a string to display as the title of the plot
  5. a cell array of strings to display below the tick marks on the x axis

The two calls to the visualize function in the test_visualize script provide examples of these inputs. Your function should loop through the rows of data in the input data matrix and display one after the other on the same figure. If you place a pause in this loop, similar to the pause in your virus function in Assignment 4, the presentation will appear as an animation, similar to the Broomberg visualization mentioned above. As you are looping through the rows of data, when the average of the newly displayed data breaks a record (i.e. the mean of the data values in the new row is higher than the mean of all previously displayed rows), a horizontal dotted line should be displayed at the new record mean data value. Finally, each row of data should be displayed with a different color. The two figures below show the full sets of data that should be visible when the animation is complete. The dotted horizontal lines show the average temperature or Dow Jones values during record breaking years or weeks, respectively. The colors change from "cool" shades of blue to "hot" shades of red as time progresses.

   

The following are a few guidelines and tips for implementing the visualize function:

  • the input data matrix should be a required input, while the other four inputs should be optional; the default values for the x label, y label, and title can be an empty string or some generic string, and the default value for the tick marks on the x axis can just be the column numbers for the data matrix
  • create a fairly large figure window for your graph, using the 'Position' property, e.g.

         figure('Position', [100 100 1000 800]);

  • in Assignment 2, you learned how to place string labels on the x axis tick marks, e.g.

         set(gca, 'XTickLabel', {'Democrats' 'Republicans' 'Independents'})

    The 'FontSize' property can also be used with the set function to set the size of the font used to display the strings. This property can also be used with the xlabel, ylabel and title functions.

  • in advance of plotting the rows of data, determine the overall range of the data and use the axis function to set appropriate ranges of values for the x and y axes, so that the axis ranges are not automatically readjusted as the animation progresses.

We have been using single characters, like 'r' and 'g', to specify the color of a plot. An arbitrary color can be specified with a vector of three values that are each between 0 and 1 and represent the amount of red, green, and blue that combine to create the desired color. When calling the plot function, this color vector can be specified with the 'Color' property, e.g.

     plot(xcoords, ycoords, 'Color', [0.3 0.8 0.7]);

which creates a plot with a teel blue color. MATLAB provides a set of built-in color palettes, or colormaps, that you can view in the MATLAB documentation on the colormap function. Each color palette has a name and is associated with a built-in function of the same name that can be used to generate an n x 3 matrix with red, green, and blue values for a set of n colors that span the palette. One of the palettes, for example, is named jet and spans the colors of the rainbow from blue to red. The function call jet(10) creates a 10 x 3 matrix storing 10 sample colors from blue to red:

>> colors = jet(10)
colors =

         0         0    0.6667
         0         0    1.0000
         0    0.3333    1.0000
         0    0.6667    1.0000
         0    1.0000    1.0000
    0.3333    1.0000    0.6667
    0.6667    1.0000    0.3333
    1.0000    1.0000         0
    1.0000    0.6667         0
    1.0000    0.3333         0
>> 

These colors were used in a loop to create the following plot of 10 horizontal lines of different colors:

You are welcome to use any of the built-in colormaps in your implementation. The above specifications for the visualize function do not create an animation that has all the aspects of the visualization of rising global temperatures at the Bloomberg website, but they capture everything that is essential to do for this problem. For up to 5 bonus points, you are welcome to add embellishments, such as additional text on the graphs as the animation progresses, or a different method for coloring the individual plots, similar to the Bloomberg example.

How to turn in this assignment

Step 1. Complete this online form.
The form asks you to estimate your time spent on the problems. We use this information to help us design assignments for future versions of CS112. Completing the form is a requirement of submitting the assignment.

Step 2. Upload your final programs to the CS server. When you have completed all of the work for this assignment, your assign5_problems folder should contain the original data files and five code files named printStudentInfo.m, mostFacebookFriends.m, getFavoriteSong.m, testStudentInfo.m and visualize.m. Use Cyberduck to connect to your personal account on the server and navigate to your cs112/drop/assign05 folder. Drag your assign5_problems folder to this drop folder. More details about this process can be found on the webpage on Managing Assignment Work.

Step 3. Hardcopy submission.
Your hardcopy submission should include printouts of five code files: printStudentInfo.m, mostFacebookFriends.m, getFavoriteSong.m, testStudentInfo.m, and visualize.m. To save paper, you can cut and paste your four code files into one script, and you only need to submit one hardcopy for you and your partner. If you cannot submit your hardcopy in class on the due date, please slide it under Ellen's office door.