As an alternative method you can utilize MATLAB's datacursormode
object, which does not block zooming/panning of your figure window.
A small example (assumes R2014b or newer):
h.myfig = figure;
h.myax = axes;
plot(h.myax, 1:10);
% Initialize data cursor object
cursorobj = datacursormode(h.myfig);
cursorobj.SnapToDataVertex = 'on'; % Snap to our plotted data, on by default
while ~waitforbuttonpress
% waitforbuttonpress returns 0 with click, 1 with key press
% Does not trigger on ctrl, shift, alt, caps lock, num lock, or scroll lock
cursorobj.Enable = 'on'; % Turn on the data cursor, hold alt to select multiple points
end
cursorobj.Enable = 'off';
mypoints = getCursorInfo(cursorobj);
Note that you may need to click inside the figure window to trigger the data cursor.
Here I use waitforbuttonpress
to control our while
loop. As commented, waitforbuttonpress
returns 0
for a mouse button click and 1
for a key press, but is not triggered by the Ctrl, Shift, Alt, Caps, Num, or ScrLk keys. You can select multiple points by holding Alt while you click.
Once you're done selecting points, hit any key that triggers waitforbuttonpress
and your data points are output by calling getCursorInfo
, which returns a data structure containing information on your points. This data structure contains 2 or 3 fields, depending on what is plotted. The structure will always contain Target
, which is the handle of the graphics object containing the data point, and Position
, which is an array specifying the x, y, (and z) coordinates of the cursor. If you have plotted a line
or lineseries
object you will also have DataIndex
, which is a scalar index into the data arrays that correspond to the nearest data point.