Attach click-event to Figure
조회 수: 53 (최근 30일)
이전 댓글 표시
I want to update a Figure in a loop and attach a mouse-click-event. The following code works fine:
Function:
function OnMouse(hObject,~)
axes_handle = gca;
pt = get(axes_handle, 'CurrentPoint');
clicked_x=pt(1,1)
clicked_y=pt(1,2)
Main:
im = rand(500,500);
f=figure(1);
set(f,'WindowButtonDownFcn',@mytestcallback)
imshow(im)
But if I update the figure in a loop, the click is (nearly) never detected:
for i = 1:100
im = rand(500,500);
f=figure(1);
set(f,'WindowButtonDownFcn',@mytestcallback)
imshow(im)
%pause(1)
%pause(0.001)
end
Including a pause of 0.5 seconds solves this problem, but I cannot afford to make pause-time > 5 ms. How can I update my figure through a loop and attach a click-event which always will be detected?
댓글 수: 0
답변 (1개)
Walter Roberson
2018년 8월 18일
f=figure(1);
set(f,'WindowButtonDownFcn',@mytestcallback)
for i = 1:100
im = rand(500,500);
imshow(im)
drawnow(); %allows display to update _and_ allows button callback to be processed
end
댓글 수: 1
Brian Wolin
2019년 5월 1일
편집: Brian Wolin
2019년 5월 1일
I found that when running Walter's code, I get unusual behavior of when the callback executes or not. When clicking on the image, the callback executes maybe 25% of the time. When clicking inside the figure, but outside the image, the callback executes every time.
Any ideas why this might be the case and/or how to ensure the callback executes every time when clicking on the image/axes?
Here is my specific the callback function code, but I don't think this part matters for the behavior I describe.
function mytestcallback(src,~)
pt = get(gca,'CurrentPoint');
fprintf('Clicked: %d %d\n', pt(1,1),pt(1,2));
end
Edit: Also note that this behavior only manifests while the loop is running. Afterwards, clicking on the image is just as reliable as clicking elsewhere.
참고 항목
카테고리
Help Center 및 File Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!