Things in purple were not explicitly covered but are still part of the

advertisement
CS 103, Spring 2006
Week 12
J. Michael Fitzpatrick Lecture Notes Page 1 of 5
Week 12 (Things in purple were not explicitly covered but are still part of the course.
Things in red pertain to Silicon Chalk (SC) or to error messages produced by Matlab)
Most of you have been using Matlab 2008a for CS 103. In one week, you will need
Matlab 2008b. To obtain it, go to Other Links and click Obtaining Matlab. Please install
it on your laptop by Monday (April 6). Then—and this is VERY IMPORTANT—type
syms x into the command window and hit Enter. Then type diff(x^2) into the
command window and hit Enter again. If either command gives an error and if the second
command does not give as the answer 2*x, then let us know. These commands must be
working for you to do symbolic mathematics (see the Schedule).
Graphical User Interfaces (GUIs)
 Give these commands in the Comman window and say that this is the traditional
command-oriented approach to programming:
 >> random_numbers = 10*randn(1,1e5);
>> hist(random_numbers,1000);
 Now run GUI_preclass (i.e., just type it in the command window). This is the GUI
version, and we are going to make this together. (See the figure below.)
 Tell the students to try to follow along, but, if they can’t get it all to work, they can
look at my file on line. Use guide to produce Section1_GUI or Section2_GUI so that
it has the following objects (leave their default tags as they are):
o One static textbox with the string “Random Generator” at a 14 pt font
o one static textbox with the string “Number Samples”
o one edit box to hold the number of samples
o one static textbox with the string “Std Dev”
o one edit box to hold the standard deviation
o one set of axes to display the histogram of the vector of samples when it is
generated
o one pushbutton with the string “Generate”
o one pushbutton with the string “Quit” (don’t put the close statement in it yet.)
 It’s a good idea to set it up as a do-nothing GUI first, then run it and note that nothing
happens. (It looks good but doesn’t do anything. I know some people like that!)
 Then, put into the callback of the quit button the command fprintf(‘Hey! Stop
that!’). Run the program and click on the Generate button and then the Quit button.
This is to show them the callback concept is working as promised.
 Change the tag of the Quit button to Quit_button. Hit the green arrow and then show
them how the name of the callback has been changed to match the tag.
 Change other tags: Generate_button, Number_Samples_edit.
 Now replace the fprintf statement with the statement close.
 Now put this in the Generate_button_callback (i.e., ignore the number of samples
and the standard deviation that are displayed in the edit boxes):
random_numbers = 10*randn(1,1e5);
hist(random_numbers,1000);
Wed, 4/1/09:
CS 103, Spring 2006
Week 12
J. Michael Fitzpatrick Lecture Notes Page 2 of 5
Approximate end of first day (These things take a long time to explain. Don’t expect to
do much more. It’s worth the time it takes, though!)
o
Wed, 4/3/09:

Show GUI_Program organization.doc
 Now look again at the code in the Generate callback and ask, “How could we
know the value of length and stddev?” Tell them that there are two possible
methods. Both methods will introduce both set and str2double.
 First approach: Read the information directly from the edit boxes:
n = get(handles.Number_Samples_edit, ’string’);
ln = str2double(n);
 Second approach uses a “pseudo-global” variables
o In Number_Samples_edit_callback():
handles.num_samp = get(hObject,’string’); % creates a new field
handles.num_samp = str2double(handles.num_samp);
o guidata(hObject,handles); % saves new handles in fig-file
o In Generate_button_Callback:
random_numbers = handles.stdev*rand(1,handles.num_samp);
 Show fig_and_handles.doc
 The pseudo-global variables are the fields of the struct handles. These
variables are copied into every function every time it is called through the
struct handles.
 Any changes inside the function are stored back into the global memory via
guidata(hObject,handles).
NOTE: This second approach does not work if the user hits the Generate
button before clicking or hitting enter inside the Number-of-Samples edit box,
because in that case Number_Samples_callback has not been called! This
problem should be demonstrated by hitting the Quit button, then hitting the
green arrow, then hitting Generate and noting that there are red error messages
in the Command Window.
o Solution: Put the same command that are in Length_edit_callback into
Length_edit_CreateFcn.
 Tell them to do same sort of thing for the standard deviation.
After getting all the above working, add a slider that changes the standard deviation.
Give it the tag Std_Dev_Slider. Here is its code:
factor = get(hObject,'Value');
handles.stdev = handles.stdev*factor;
set(handles.Std_Dev_edit,'String',num2str(handles.stdev));
guidata(hObject,handles);

The final result should look something like this
CS 103, Spring 2006
Week 12

J. Michael Fitzpatrick Lecture Notes Page 3 of 5
To make is really slick, add this statement at the end of the callback for the slider and
run:
Generate_button_Callback(hObject, eventdata, handles)
The plot is now updated every time the slider is moved (when the mouse button is
released).

Then, add exactly the same command at the end of the two edit boxes, and run again.
Note that the callback for an edit box is called if and only if (a) the mouse is clicked
inside the box, and then (b1) enter is hit, or (b2) the text is changed and then enter is
hit, or (b3) the text is changed and then the mouse is clicked outside the box.
Here are the concepts that should be covered while building the GUI:






Matlab’s GUI Development Environment = guide
GUI is defined by two files: name.fig and name.m
Each component of a GUI is an Object
An object has a set of properties called its property list.
o You can view and change these properties
 with the Property Inspector during programming
 with functions while the program is running:
 get()
 set()
event
o mouse click (left, right, middle, scroll)
o keyboard press (or release)
o other—eg, serial line high or low
callback
CS 103, Spring 2006
Week 12
J. Michael Fitzpatrick Lecture Notes Page 4 of 5
o




function that is called automatically upon a specific event
 Each object in a GUI has one callback
handle
o address of memory at which information about an object is stored
 See fig_and_handles.doc
Functional access to object properties
Sharing information among callback functions
o Cannot be done with the usual mechanism of passing arguments
o Use new fields in a variable called handles
 Storing information by one function
 handles.my_field = value;
 guidata(hObject, handles);
 Reading information by another function
 copy_of_value = handles.my_field
GUIs (continued):
o Use GUI_demo.m
 run it and show the functionality, but omit the Add button.
 run guide
 Explain that the “generate” button reads the contents of two
other objects to find the length and maximum value of the
random vector.
 use GUI_demo_summary.m to explain essentials of GUI_demo.m
 It has only the primary function and the callbacks and all the
comments provided by Matlab are gone.
 Generate_Button_Callback
o Looks up vec_len in the properties of Length_Edit
and vec_max in the properties of Height_Edit.
o Other actions described below.
 Length_Edit
o It’s initial ‘String’ property value is 10.
o It’s callback function does nothing!
 Height_Slider_Callback
o Must look up ‘Value’ property of its own object!
o Doesn’t get it automatically, but…
o Does get to use hObject, instead of
handles.Height_Edit.
o Changes the ‘String’ property of Height_Edit
 Height_Edit_Callback
o Must look up its own ‘String’ property (initially 10)
o Clips it into the Min-Max range of Heght_Slider
o Changes the ‘Value’ property of Height_Slider
 Note how the objects Height_Edit and Height_Slider have
their respective ‘String’ and ‘Value’ properties synchronized.
 Generate_Button_Callback
CS 103, Spring 2006
Week 12
J. Michael Fitzpatrick Lecture Notes Page 5 of 5


Mon, 4/6/09:
o Looks up ‘SelectedObject’ in Style_Group
 The handle of the most recently pressed button
is stored there.
 Compare it with known handles in handles
Plotting
o Note the new argument (1st one), handles.axes1 given
to plot, which is the handle of the axes in which to plot.
o This is a feature of plot that we have not needed until
now.
Searching
o We will study two types:
 Sequential search (sometimes called “linear search”)
 Binary
o Discuss method for searching a phonebook versus sequential search, also known
as “linear” search
 Sequential search is the only choice for unsorted data.
 Show how sequential search works with cards.
 Binary is the better choice for sorted data.
 Binary takes only about 20 tries for 1 million names in a phone book.
 Show how binary search works with the cards. Do a couple examples that
are on the list.
 For searching one unsorted list one time: sequential is better than sorting
followed by binary.
o See sequential-search algorithm in Chapter 10 from textbook
 Show the function sequential_search in the Editor Window. Put a break at
the beginning. Talk through sequential_search([1 4 3 5 6], 4).
 This search is so easy, there is no point spending more than 10 minutes on it.
o Put cards on board.
 There are eleven cards: 10,36,45,96,100,112,144,165,190,200,365. Have two
out-of-order pairs, one near the middle. Have the cards facing the board, so
that their values cannot be seen. Tell the class that I can show them two cards
at a time for comparison, just like the computer can do. Ask for someone to
tell me how to check to see whether they are in order: ANSWER: Check each
pair of neighbors.
 Put the cards in order facing the class.
 Write the index above each card on the board.
 Do binary search with the target = 112 (One check. No recursion required).
 Do binary search with the target = 190
Download