ginput

Records the mouse position(s)/keyboard input(s) in the current figure.

Syntax

[x, y, b] = ginput()

[x, y, b] = ginput(max)

[x, y, b] = ginput('parent', handle)

[x, y, b] = ginput(max, 'parent', handle)

Inputs

max
Maximum number of mouse clicks or keyboard inputs in the current figure to record The GUI will be in a paused state till the max inputs have occured in the current figure area or if the Stop button, return, escape keys or Control + C keys are pressed.
Type: integer
handle
Optional input which specifies the handle of a ui element that will be used as a parent (reference) when calculating the ginput positions. The positions printed will be normalized values and can be used when creating GUI elements, which are children of handle.
Type: double

Outputs

x, y
The positions of the mouse/keyboard presses in the current figure.
If the mouse is over a 2D plot, x and y are in the plot's coordinates system, which is defined by the x and y axes.
If the mouse is not over a 2D plot, then x and y are in the figure's coordinate system where the axes exists, the (0,0) point is in the upper-left corner of the figure window.
Type: double
b
Information on the button pressed when recording the position(s).
1 is returned for a left mouse button press, 2 for a middle mouse button, 3 for right mouse button and ascii value for any keyboard input.
Type: integer

Examples

ginput is used retrieve a specific number of points on a plot picked with a combination of mouse clicks and keyboard inputs:

plot(rand(100,1));
[x, y, b] = ginput(3)

x = [Matrix] 1 x 3
18.67280  72.99380  67.43830
y = [Matrix] 1 x 3
0.68790  0.44374  0.86624
b = [Matrix] 1 x 3
1  103  1
ginput is used retrieve normalized positions on a figure to help in placing GUI elements on the figure.

f = gcf();
[x, y] = ginput(3, 'parent', f)
p = uipanel(f, 'units', 'normalized', 'position', [x(1), y(1), x(2)-x(1), y(3)-y(2)]);