CSc 4630/6630 Programming Challenges

1. Due August 30
At the MATLAB command prompt, type
        disp('Hello X');
only replace the X with your name. Then use the disp command to display text about where you are using MATLAB, like "GSU lab in room Y of building Z."


2. Due September 6
At the MATLAB command prompt, make a range from 100 to 110, but only even numberss, and store it in a variable called "myrange". Make an array with 4 rows and 3 columns, and call it "myarray". Fill it with any numbers that you like, but make each matrix element unique. Then show the lower-right, 2x2 corner of the array.


3. Due September 13
Display the text "odd numbers 11 to 17". Next, use a "for" loop to display odd numbers between 11 and 17. Then display the text "even numbers 4 to 12", and use a "while" loop to display even numbers between 4 and 12. You should use the "disp" command to display the text and numbers.


4. Due September 20
Make a function (i.e. with the "function" keyword) called "pc4", and store it as "pc4.m". The function should have 2 inputs and 1 output. It should set the output to the first input plus two, times the result of the second input minus four. Here are a couple of examples.

        >> pc4(5,7)
       
        ans =
       
        21
       
        >> pc4(12,11)
       
        ans =
       
        98

Use the "type" command to print the function's contents to the screen (i.e. "type pc4.m"), and show the function works for at least 3 test cases. Turn in a copy of the interaction, i.e. copy from the MATLAB command window, or use the diary command.


5. Due September 27
Make a function, called "pc5.m", to convert the day of the week from a number to a string. It should have one input, an integer value, that specifies the day, i.e. 1, 2, 3, 4, 5, 6, or 7. It should return one output, a string, i.e. Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, or Saturday. Use a "switch" statement to choose the output string. Note that the function should normally not print anything, unless the input is a value not listed above. In that case, it should print a message using the "error" command.

Like earlier assignments, use the "type" command and several test cases to show that your function works. Turn in a copy of the interaction, i.e. copy from the MATLAB command window, or use the diary command.

Example:

        >> dayname = pc5(1);
        >> disp(sprintf(' The day is %s.', dayname));
        The day is Sunday.


6. Due October 4
(Note: I am posting this early. You are NOT expected to do pc6, or even read it, before the exam.)
MATLAB provides another type of "for" loop, called "parfor", in its Parallel Computing toolbox. As you might guess, "par" is short for parallel, meaning that the work of the "for" loop is sent to multiple processors. Here is an example.

        >> arr = 1:100;
        >> parfor i=1:length(arr)
        b_array(i) = sqrt(arr(i));
        end
        Starting parallel pool (parpool) using the 'local' profile ... connected to 4 workers.

Note that the "Starting parallel pool..." message is the computer's response, not something that you type.

Make a script, called "pc6.m" to do the following. First, define array "arr", like above. Then, use a "try" block to do the "parfor" example that you see above. In the "catch" block, do the same thing, only with a regular "for" loop. This way, if the "parfor" creates an error, your computer can still do the calculation, just in a slower way. Then, create a second "try" statement, with a non-existent function, like "qwerty()", and in the "catch" part, display "function qwerty does not exist."

Show your program (i.e. with "type"), and show it run.


7. Due October 11
Here is a sound file. Save it under your MATLAB directory (on a Mac, press the "control" key and click the mouse to show options), then use the "audioread" (or "wavread")* command to store the data in a variable. Store the sampling rate in a variable, too, since you will need it. Then use the "sound" command to play it.

As you should be able to hear, the message is unclear. In fact, it is backwards. You will need to reverse the array to straighten it out. Do that, and play the new version with the "sound" command. Next, use a "disp" command to print "the word or phrase is:" and then the word or phrase from the reversed data.

Here is a hint: suppose "old_array" has 100 values. We could access array values 1 to 100 with the command old_array(1:100). We can reverse that array with this:
        >> new_array = old_array(100:-1:1);


* The italicized text is altered from the original version of this assignment, since "wavread" has been replaced by "audioread" in the latest versions of MATLAB.


8. Due October 18
Here is an image file. Use MATLAB to read the image, show it as a figure, then have the user select 2 points, crop the image, and show it as another figure. Don't worry, this is actually easy to do.

First, read the image and display it. You will need to define "filename" as a string of the filename.
        x = imread(filename);
        imshow(x);
Now have the user (you) select 2 points.
        title('Select 2 points');
        [a, b] = ginput(2);

Now, you will need to "clean up" variables "a" and "b". Inspect the values, and make sure they are integers within the boundaries of "x", changing them as needed. Then make a cropped image:
        y = x(r1:r2, c1:c2);
        figure();
        imshow(y);
        title('cropped version');

Once you have this working, crop it so that the lower-right corner is showing, about 1/4 of the original size.


9. Due October 25
Download this small image, load it onto a variable with the "imread" command, then use the "im2bw" command to make it a logical image (i.e. image2black and white). Next, reverse the result, so that the black and white colors are swapped. Then show it with the "imshow" command. Use the command "text(10,10, 'cells');" (you will see what it does). Use "title", "xlabel", and "ylabel" to add information to the figure, where the title is the filename of the image, the label on the x-axis is "pc9", and the y-axis label is your name. Print this image, and show your code.


10. Due November 1
Make a GUI with a callback function to show a character of the alphabet, and change it according to a slider input.

Display a character of the alphabet on a figure as a text element, starting with 'A'. Also show a slider. As the user moves the slider from left to right, show another character, in increasing order, up to 'Z'. Show it in decreasing order if moved from right to left. You may want to investigate "SliderStep" as well as the "Min" and "Max" of the slider.

Turn in the MATLAB program, the callback function, and a screen capture of the figure showing the first letter of your last name.


11. Due November 8
There is a data file here to be used with this assignment. Download it, and "load" it into your MATLAB session. It defines a structure with counts of different coins, along with their values. Calculate the total value, store that as part of the structure, and "save" a new version of the data file. Your solution should show the computed total value.


12. Due November 15
Write a program to open a file called "mydata", read the contents, up to the first 200 characters, and close the file. Have your program go through the data that it read, changing any number less than 32 to the value 32. Then display the data as a string. (The value 32 is the ASCII code for a space. This means that your program will replace any non-printable characters with spaces.)


13. Due November 29
This is the last one for the semester!
By this point, you have seen how MATLAB can load in sound files, images, genetic data, and variables defined in a previous session, as well as read other files. It can also read in spreadsheets, and that is what this assignment is about.

You can think of a spreadsheet as a matrix of numbers, though spreadsheet software also allows you to enter strings. CSV stands for comma separated values, and is a very portable type of spreadsheet. We will use the "csvread" function, perform some math on the data, and write it back with the "csvwrite" function. From the MATLAB help, use commands like "M = csvread('FILENAME')" and "csvwrite(FILENAME,M)". Of course, you can and should use your own variable names.

Read the CSV data from the file "somedata.csv" into a variable. Find the average for each row as an integer, and store that as a new column. Then write the data, including the new column, to a file called "somedata2.csv". Use "type" to display the contents of the new file.


Copyright 2016, Michael Weeks