CS 112

Assignment 6

Due: Friday, April 1, at the beginning of class

You can turn in your assignment up until 5:00pm on 4/1/10 without penalty, but it is best to hand in the assignment at the beginning of class. Your hardcopy submission should include a cover sheet, printouts of two code files: drawGUI.m and popGUI.m, and a hardcopy of a picture created with the GUI sketchpad that you construct for Problem 1. Your electronic submission is described in the section Uploading your saved work

Reading

The following material is especially useful to review for this assignment: notes and examples from Lectures #12-14, and Lab #7. There is also extensive online documentation on the creation of graphical user interfaces with the GUIDE program available through the MATLAB Help facility.

Getting Started: Create an assign6_programs folder

There are no initial programs needed for this assignment, as you will be creating two GUI-based programs from scratch. Create a folder for your programs that includes assign6_programs in the name, e.g. sohie_assign6_programs. In MATLAB, set the Current Directory appropriately.

Uploading your saved work

Use Fetch or WinSCP to upload your saved work, and connect to the CS file server using your personal user account name and password. After logging in to your account:

When you are done with this assignment, you should have four files stored in your assign6_programs folder: two M-Files, drawGUI.m and popGUI.m, and two .fig files, drawGUI.fig and popGUI.fig.

Problem 1: Create a GUI sketchpad

In this problem, you will build a GUI sketchpad that might look something like this when complete:

Part A: Using the Layout Editor to lay out your GUI

To begin, enter guide in the Command Window to start the Layout Editor for the MATLAB Graphical User Interface Development Environment. A GUIDE Quick Start window will appear with Blank GUI (Default) selected. Click the OK button. A Layout Editor window will appear (image shown below), titled untitled.fig (when you save your GUI for the first time, name it drawGUI). Select Preferences... from the File menu, and in the Preferences window, check the box labeled Show names in component palette and click the OK button. You are now ready to create your first GUI!

Guide layout window

First, you're going to design the physical layout of your sketchpad, using the components listed below. The GUI image above is just a sample -- you are free to layout your own GUI as you wish.

For each component, do the following:
General tips for working with GUIs

Part B: Writing MATLAB code to add functionality to your GUI components

Once you have your GUI components laid out, you can then modify the drawGUI.m code file to perform actions in response to each of the GUI components. Recall that MATLAB automatically generates code for each GUI component in your drawGUI.m file. First, modify the drawGUI_OpeningFcn function (executed when the GUI is first created) by constructing fields of the handles structure that store all of the information that needs to be shared across the GUI components:

Think carefully when constructing your unit shapes above. The GUI allows for repositioning (the user supplies x and y position) and scaling (the user supplies figure size) of these shapes, so your unit shapes must support these actions.

Your next step is to modify the Callback functions for the components that have an immediate visual effect in your GUI. For example, the plot button has an immediate effect when pressed in the GUI. For the other GUI components that do not have an immediate visual effect (e.g., x position, y position, etc), MATLAB automatically keeps track of the current values of the user-entered strings.

Notes:
  1. Remember that every function that modifies the value of a field of the handles structure must include the following statement at the end:

        guidata(hObject,handles)
  2. Many Callback functions will remain empty, that is, no action will be performed when the GUI components are used. The GUI component information (e.g. x position) is accessed by using the handles structure from within the Callback function for the plot button.
  3. You can also include printing statements by omitting the semi-colon at the end of any statement that produces a value, or adding calls to the disp function. These values will be printed in the Command Window.

Part C: I think my GUI is done, now what do I do?

Thoroughly test your GUI to make sure it really works.

Nothing to undo?

Check your GUI in the following scenario. If your hold on button is clicked and you plot something, and then click undo, what you just drew should go away. Now, if you click undo again, but there is nothing to undo, a well-designed GUI would handle this scenario gracefully (i.e. without generating MATLAB error messages). Your GUI should handle this scenario gracefully.

View the whole drawing from the beginning

MATLAB's tendency is to show just what you plot and no more. When drawing in a sketchpad, it is often desirable to view the entire drawing canvas for the session. One way to do this is to create a rectangle that has the dimensions of your desired drawing (e.g. 12.5 x 9.25 in the above GUI), and draw that rectangle in white so it is invisible. This will initialize the plotting area to the specified dimensions. Of course, if the user plots something out of that canvas, (e.g. a bird at coordinates (20,20)), then MATLAB will automatically expand the canvas to include the new object.

Design a picture and print a hardcopy

Fully test your GUI by experimenting with many settings. When testing, remember that you should hit the return key after editing any text field of your GUI. When you are convinced your GUI is working well, construct a final picture to submit with your assignment. Print a hard copy of your final picture (see instructions below) to turn in with your assignment.

How to print a hard copy of your final picture in your sketchpad

Electronic submission

Be sure to include both the drawGUI.m and drawGUI.fig files in the assign6_programs folder that you submit electronically.

Problem 2: Simulating Population Growth

Population growth refers to how a population increases or decreases over time. This growth is controlled by the birth and death rates of the population. If a population has a constant birth rate over time and is never limited by factors such as food or disease, it exhibits exponential growth, which is captured mathematically by the following equation:

    pt+1 = r * pt

where pt is the population in generation t, pt+1 is the population in generation t+1, and r is the growth rate between generations.

Real populations are limited by factors such as food and disease, creating an upper limit on the size of the population that the environment can support. Ecologists refer to this as the carrying capacity of the environment. The type of growth that occurs in this scenerio is referred to as logistic growth and is captured mathematically by the following equation:

    pt+1 = r * pt * (K - pt)/K

where K is the carrying capacity.

In this problem, you will create a GUI based program that allows you to visualize the results of simulating these two types of population growth for different values of the parameters. The following is a sample GUI display for this program, named popGUI (your GUI does not need to be laid out in exactly the same way):

The display area on the left shows two individuals (randomly positioned dots) in an initial population in the first generation. The three text boxes allow the user to enter values for the growth rate, carrying capacity, and number of generations to be simulated. The button labeled logistic is a toggle button that is used to select the exponential or logistic models. The close button closes the window and terminates the program. The remaining three buttons cause the following actions to be performed:

The following display shows the results after a simulation of 25 generations of the logistic growth model:

Some tips on completing your program:

Electronic submission

Be sure to include both the popGUI.m and popGUI.fig files in the assign6_programs folder that you submit electronically.