'CurrentPoint' fails in GUI when function is called by 'ButtonDownFcn'

2 次查看(过去 30 天)
I have a GUI with an axis. I would like to be able to click and drag on the axis and have the start and end points detected in the units of the axis.
For a blank GUI with axes1 placed:
Code in OpeningFcn
% --- Executes just before currentpointtest is made visible.
function currentpointtest_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for currentpointtest
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
I = imread('cameraman.tif');
h = imshow(I, 'Parent', handles.axes1);
set(h, 'ButtonDownFcn', @getClicks)
Code in Callback:
function getClicks(~,~)
point1 = get(gca,'CurrentPoint') % button down detected
finalRect = rbbox; % return figure units
point2 = get(gca,'CurrentPoint') % button up detected
point1 is correct, but point2 returns the same value as point1. I need the values relative to the axes, so 'PointerLocation' doesn't seem like an option. Also as a side note, if the getClicks function is called from a push button everything works fine.
Does anyone know what is happening here and how I can get both point1 and point2?
Update(2012-03-07): I also tried the following:
function getClicks(~,~)
point1 = get(gca,'CurrentPoint') % button down detected
set(gcf, 'WindowButtonUpFcn', @upCall)
finalRect = rbbox; % return figure units
set(gcf, 'WindowButtonUpFcn', '')
point2 = get(gca,'CurrentPoint') % button up detected
function upCall(~,~)
upPoint = get(gca,'CurrentPoint')
This returns point1, then point2 (which is the same as point1) and finally upPoint (which is correct), in that order.

回答(2 个)

Walter Roberson
Walter Roberson 2012-3-7
Read the section "CurrentPoint and Cursor Motion".
axes ButtonDownFcn is not one of the events that triggers CurrentPoint update.
You might be able to improve the situation by adding a drawnow() call after the rbbox .

Dirk Engel
Dirk Engel 2012-10-22
Maybe the "Interruptible" property of the ancestor figure is set to "off". This was responsible for a missing update of the CurrentPoint in my case.

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by