Lab 22

Structure Arrays

CS211 Lab Policy:

Instructions:

You will create one MATLAB program named Lab22() in file Lab22.m for this lab.  The instructions below ask you to perform the same tasks as the previous lab assignment, Lab 21, but this time you will use data that is stored in a structure array.

Open your Lab21.m file and use the "Save as..." command to save it into a new file called Lab22.m. Modify this new file for this assignment.

Modify the name of your function to Lab22(). Then select all of the statements in the body of the Lab22()function and use the Text menu, Comment command, to comment out all of the statements. You will then un-comment and modify sections of the code as described below.

  1. Clear the command window.
     
  2. Use the MATLAB statement below to read the Marathon race data from a file into a set of arrays. (Replace the 3 MATLAB statements that read the data file with this single statement.)

    [Overall_position Age_position Age_total Name Age Gender Town State Ascent Descent Total] ...
       = textread('PikesPeakMarathon2007.txt', '%d %d%*c%d %24c %d %c %15c %2c %7c %7c %7c');

     
  3. Use the whos command to display information about the arrays that currently store the data.
     
  4. Use a struct() function call to create a structure array called Runner that defines a field for each of the data arrays created by the textread() function. You should define 11 fields. Make the size of the structure array be equal to the length of one of the arrays. (Why does it not matter which array you use?) Set every field value for every element to [] (an empty vector).
     
  5. Re-organize the "parallel arrays" into a single structure array using a loop similar to the one shown below. Fill in appropriate statements for the fields not shown using the field names you created in step 4. Use the strtrim() function on all string data to remove their leading and trailing blanks. This can save significant amounts of memory.

        for J = 1 : length(overall_position)
            Runner(J).finish_position = Overall_position(J);
            Runner(J).age_finish      = Age_position(J);
            ..
            Runner(J).name            = strtrim( Name(J,:) );
            ..
        end

     
  6. Use the whos Runner command to display information about your structure arrays. Does the displayed information make sense to you?  If you have any questions, please ask your instructor.
     
  7. Before you begin plotting, make sure you can get specific data values out of the structure array. Using fprintf(), display the following values:
  8. Now it's time to plot! Un-comment a group of statements that performs a plot, modify its syntax to correctly use the structure array, and then verify that it creates a correct graph. After this section of code is working properly, move on to the next plotting task.
     

Turn-in:

Submit your Lab22.m file.