Lab 27
Application Data,
Finding Objects and Mouse Selection
CS211 Lab Policy:
- This lab exercise will not be graded.
- Submit as much as you have completed before the end of the lab period in
which it is assigned.
- If you do not finish this lab work, it is to your advantage to finish it
outside of class. Please re-submit your finished work to the course web
site.
- You may receive help from anyone in completing this lab.
- You may not submit another student's code as part of your
lab.
Instructions:
For this lab you will create and submit the program
Lab27() saved in the file
lab27.m. This program will plot 3 separate
curves in a figure window and then let the user select which one is "labeled"
and highlighted. Your Lab27() program should have the following behavior:
- If the user clicks the left-mouse-button close to a particular line
graph, then that line graph should become bold (have a
LineWidth of 3).
- If the user clicks the right-mouse-button close to a particular line
graph, then that line graph should become red (have a
Color of [1 0 0]).
- The legend should only display the equation of the selected line graph.
- If there is no "current object," none of the line graphs should be
highlighted.
- If the user presses the 'q' key, the program should close the figure
window and stop execution. All other key presses should be ignored.
Your program should perform the following tasks:
- Clear
the command window.
- For x values in the range [-4pi, 4pi], plot the curves
sin(x),
2sin(2x), and x(sin(x))
using 3 separate plot()
function calls. (Use hold('on')
to keep the plots in the same axes.) Capture the handle
to each
line object and store each handle in a unique variable.
- Initialize a variable called Key
to a single space (' ').
- Create a while loop that executes
as long as Key is not equal to a
'q'. Perform the following tasks for
each iteration of the loop:
- Call waitforbuttonpress() and
capture its return value.
- If the waitforbuttonpress()
returned a zero:
- Set the 'LineWidth' and
'Color' properties of all
three line graphs to 1 and
'blue' respectively.
- Remove the legend from the graph using the command
legend('hide').
- Get a handle to the "current object" using
gco().
- Get the type of mouse button that was pressed by using
get() to get the
'SelectionType' property of
the figure window.
- If the handle to the "current object" is equal to the handle of
the sin(x) curve:
- Set the legend to 'sin(x)'
- If the left-mouse-button was pressed, change the
sin(x) curve's
'LineWidth' property to
3.
- If the right-mouse-button was pressed, change the
sin(x) curve's
'Color' property to [1 0
0].
- If the handle to the "current object" is equal to the handle of
the 2sin(2x) curve:
- Set the legend to '2*sin(2x)'
- If the left-mouse-button was pressed, change the
2sin(2x) curve's
'LineWidth' property to
3.
- If the right-mouse-button was pressed, change the
2sin(2x) curve's
'Color' property to [1 0
0].
- If the handle to the "current object" is equal to the handle of
the x(sin(x)) curve:
- Set the legend to 'x*sin(x)'
- If the left-mouse-button was pressed, change the
x*sin(x) curve's
'LineWidth' property to
3.
- If the right-mouse-button was pressed, change the
x*sin(x) curve's
'Color' property to [1 0
0].
- If the waitforbuttonpress()
returned a one:
- Use a get() command to
set the Key variable to the
'CurrentCharacter' property
of figure window. (Use gcf()
to get the figure's handle.)
- Close the figure and exit the program. (This happens after the
while loop exits.)
Turn-in:
Submit your
Lab27.m file.