Say you want to have the user select some portion of the data interactively using the mouse. You can get the coordinates of a user's clicks by using the Matlab command ginput.
I added this code to my script so that you can tell the user what to do:
fprintf(1, 'click on lower left and upper right of the area of interest\n')
Note that the
\n
means "add a new line after this."
NOTE: fprintf is a function that is used to write out text (to a file or the command window).
The "1" as the first argument in fprintf tells the function to print the text to the screen.
The argument of 2 to ginput tells it to take two clicks. When you move the mouse over the figure, you will see that the cursor turns into a moving cross hair:
The result in that case is:
click on lower left and upper right of the area of interest
xmin =
-110.4103
xmax =
-110.2831
ymin =
44.4732
ymax =
44.5826
Let's draw a red box then around that selection:
%this draws a box. Realize that it has 5 vertices because you have to close it back around to the beginning
xs = [xmin xmax xmax xmin xmin];
ys = [ymin ymin ymax ymax ymin];
hold on
plot(xs, ys, 'r-')
Finding the events within a user-defined box
Now for some real interactivity. Find the events inside that square:
And, let me teach you about another useful interactive item: Matlab command gtext. This will let us label our box:
NOTE: input is a function that lets you write out a message or question to the screen and ask the user for input. The user can then type anything, and that input is then assigned to the variable specified by input. If you want to get really fancy, tryout the function inputdlg. This gives you a popup box to write your response.
your_label = input('What is your label for that selection? \n', 's');
gtext(your_label, 'Color', 'r')
Here I have introduced the concept of subplot.
By calling subplot we can place multiple axes into the same figure window and specify where in the window we want the axes to be plotted.
Which of the above did you like better? Do you see what was different?