Drawing with mouse motion in a GUI-image
5 次查看(过去 30 天)
显示 更早的评论
Hi, I want to draw a line in an image displayed in a GUI. Matlab has a good example which works great for an individual figure:
However, while running it from a callback function in the GUI, I am having issues:
- Defining handle for the image-axis is not allowed by WindowButtonDownFcn.
- I can use the handle of the GUI-figure (which is an alternative of the point above), but how do I extract the co-ordinates for later use?
Example code (Matlab)
function draw_lines
% Click the left mouse button to define a point
% Drag the mouse to draw a line to the next point and
% left click again
% Right click the mouse to stop drawing
%
figure('WindowButtonDownFcn',@wbdcb)
ah = axes('DrawMode','fast');
axis ([1 10 1 10])
function wbdcb(src,evnt)
if strcmp(get(src,'SelectionType'),'normal')
[x,y,str] = disp_point(ah);
hl = line('XData',x,'YData',y,'Marker','.');
text(x,y,str,'VerticalAlignment','bottom');drawnow
set(src,'WindowButtonMotionFcn',@wbmcb)
elseif strcmp(get(src,'SelectionType'),'alt')
set(src,'WindowButtonMotionFcn','')
[x,y,str] = disp_point(ah);
text(x,y,str,'VerticalAlignment','bottom');drawnow
end
function wbmcb(src,evnt)
[xn,yn,str] = disp_point(ah);
xdat = [x,xn];
ydat = [y,yn];
set(hl,'XData',xdat,'YData',ydat);
end
end
function [x,y,str] = disp_point(ah)
cp = get(ah,'CurrentPoint');
x = cp(1,1);y = cp(1,2);
str = ['(',num2str(x,'%0.3g'),', ',num2str(y,'%0.3g'),')'];
end
end
2 个评论
Image Analyst
2016-7-18
Are you using GUIDE? Or are you trying to do it the hard way and define all the properties/settings and callbacks yourself?
And what's in the axes control? If there is something like an image you'll have to set a callback for the image since it lies on top of the axes, or else set the hittest for the image to be off.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!