Assignment 7
|
Due: Friday, April 24th
This assignment contains one programming exercise on creating thumbnail images, and one programming problem on the analysis of data on the women's marathon in the summer olympics. These programming tasks provide practice working with strings, cell arrays, and external files. You can turn in your solutions any time on 4/24/20, and only need to submit an electronic copy of your code files. Your electonic submission is described in the section Uploading your completed work (there is no Google form to submit for this assignment). If you need an extension on this assignment, please contact Ellen or Stella. You'll start this assignment work in lab with a partner, but can continue to work independently to complete the assignment.
Reading
The following material in the Gilat text is useful to review for this assignment: 53-55, 103-110. You should also view the lecture videos on strings and cell arrays and reading and writing text files.
Getting Started: Download assign7_files
Use Cyberduck to download a copy of the assign7_files
folder
from the download folder in the course directory on the CS file server.
The assign7_files
folder contains a subfolder of images for the
programming exercise, a text file named party.txt
that you will work with in
lab, and a text file named olympics.txt
for the programming problem on this
assignment.
Uploading your completed work
When you are done with this assignment, your assign7_files
folder should
contain a new subfolder named thumbs
with a set of thumbnail images created
in the exercise, and six code files: reduceSize.m, testReduce.m, medalsYear.m,
medalsCountry.m, fastestMarathon.m,
and testOlympics.m
.
Use Cyberduck to connect to the CS file server and navigate
to your cs112/drop/assign07
folder. Drag your assign7_files
folder to this drop folder. More details about this process
can be found on the webpage on Managing Assignment Work.
Exercise: Creating Thumbnails for a Folder of Images
This exercise uses a set of MATLAB functions related to managing files and directories:
pwd, filesep, dir,
and mkdir
.
You can also explore these functions in the MATLAB Help system. Before starting, set
the Current Directory to the assign7_files
folder.
Write a function named reduceSize
that has two inputs: (1) a matrix that
stores an image and (2) a scale factor. This function should have a single output that is a
new image matrix. The output image should be a reduced version of the input image,
created by sampling the rows and columns at regular intervals given by the input scale
factor. For example, given the following function call:
smallMatrix = reduceSize(originalImage, 4);
the resulting image should be one fourth the size of the original image,
created by sampling every 4th row and column of the original image. The reduceSize
function can be very short!
Write a script named testReduce.m
that tests your reduceSize
function for a set of images stored in the images
subfolder that is
contained inside the assign7_files
folder. This script should use the
dir
function to create a listing of all of the files stored in the images
subfolder that have a filename extension of .jpg
. It
should then create a new folder named thumbs
in the assign7_files
folder. Finally, it should loop through all the image files and perform the following
three steps for each one:
- read the image from the
images
folder into the MATLAB workspace - reduce the image by a factor of 4
- store the reduced image in the
thumbs
folder (using theimwrite
function).
Problem: Women's Marathon in the Summer Olympics
In this problem, you'll define three functions and create a testing script that explore
data on the medal winners in the women's marathon in the summer olympics, an event that began
in 1984. The olympics.txt
text file in the assign7_files
folder
contains data that includes the year, medal, name, country, and finishing time for
the winners, as shown in this excerpt:
year medal firstName lastName country time 1984 gold Joan Benoit USA 2:24:52 1984 silver Grete Waitz Norway 2:26:18 1984 bronze Rosa Mota Portugal 2:26:57 1988 gold Rosa Mota Portugal 2:25:40 1988 silver Lisa Martin Australia 2:25:53 1988 bronze Katrin Dorre EastGermany 2:26:21 ...
(1) Create a testing script
To begin, create a script named testOlympics.m
and add code to read the data
from the olympics.txt
text file using the fopen, textscan,
and
fclose
functions. As you complete the three functions below, add code to this
script to test these functions.
(2) Print data for a particular year
Define a function named medalsYear
that has two inputs: (1) the cell array
of data returned by the textscan
function that includes information on the
year, medal, name, country, and time, for all of the medal winners, and (2) a year. This
function should print information about the three medal winners for the input year, using
the fprintf
function to show this data in a nice format. This information can
just be printed in the Command Window. In the following example, the cell array of data is
called medals
:
>> medalsYear(medals, 2008) women's marathon winners in 2008: gold Constantina Tomescu Romania 2:26:44 silver Catherine Ndereba Kenya 2:27:06 bronze Zhou Chunxiu China 2:27:07
(3) Write information for a particular country to a text file
Define a function named medalsCountry
that has three inputs: (1) the cell array
of data returned by the textscan
function that includes information on the
year, medal, name, country, and time, for all of the medal winners, (2) a country name, and
(3) a file name. This function should write information to a text file with the input file
name, about all of the medal winners from the input country. The fprintf
function
should be used to write this data in a nice format. For example, the following function call:
>> medalsCountry(medals, 'Kenya', 'KenyaMedals.txt');should create a new file named
KenyaMedals.txt
with content that looks something
like this:
women's marathon winners from Kenya 2000 bronze Joyce Chepchumba 2:24:45 2004 silver Catherine Ndereba 2:26:32 2008 silver Catherine Ndereba 2:27:06 2012 silver Priscah Jeptoo 2:23:12 2016 gold Jamima Sumgong 2:24:04
(4) Who holds the olympic record for the women's marathon?
Define a function named fastestMarathon
with a single input that is the cell
array of data returned by the textscan
function. This function should print information
about the olympic record holder for the women's marathon. You can use either the disp
or fprintf
functions for this printout. For example:
>>fastestMarathon(medals) The olympic record for the women's marathon is 2:23:07, set by Tiki Gelana from Ethiopia in 2012 >>
Note: the str2num
or str2double
functions in MATLAB can be
used to convert the string representation of a number to its numerical representation.