MATLAB: Impoly no longer animates the tracing line after placing other animated line objects

graphicsImage Processing ToolboxMATLAB

Our users need to trace around a region on an image using impoly (closed = 0). In some cases they would like to start at a point that aligns with a separate feature in another part of the image. So they asked for a movable, vertical "guide line" to help with the alignment.
Code:
hFig = figure;
hImg = imshow(imgFileName);
lineColor = [0 1 0.89];
lineWidth = 2;
ax=gca;
szImg = size(hImg.CData);
imgCenter = 0.5*szImg(1:2);
%%
origButtonMotionCB = get(hFig, 'WindowButtonMotionFcn');
hLine = xline(ax, imgCenter(1), '--', 'Color', lineColor, 'LineWidth', lineWidth);
set(hLine, 'ButtonDownFcn', @(obj, eventdata)fcnStartDragLine(gcbo, eventdata, hFig));
set(hFig, 'WindowButtonUpFcn', {@fcnStopDragLine, hFig, origButtonMotionCB});
function fcnStartDragLine(hObject, eventdata, hFig)
hLine = hObject;
set(hFig, 'WindowButtonMotionFcn', {@fcnDragLine, hFig, hLine});
end
function fcnDragLine(hObject, eventdata, hFig, hLine)
ax = findobj(hFig, 'Type', 'axes');
pt = get(ax, 'CurrentPoint');
ptX = pt(1, 1);
set(hLine, 'Value', ptX);
end
function fcnStopDragLine(hObject, eventdata, hFig, origButtonMotionCB)
set(hFig, 'WindowButtonMotionFcn', origButtonMotionCB);
end
Problem: After drawing the guide line, I launch impoly, however the animated line that extends from the previous vertex to the mouse pointer no longer appears. Everything else works per spec( i.e., I can still add new vertices, etc).
If I launch impoly before adding the guide line, then the animated line appears during the polygon creation.
If I delete the guide line from the axis.Children, impoly still has no animated line.
What am I missing?
Thank you in advance for any help you can offer.

Best Answer

JK from the Matlab Support Team provided a solution. Before the origButtonMotionCB definition above, he suggested adding the following code:
if isempty(get(hFig, 'WindowButtonMotionFcn'))
set(hFig, 'WindowButtonMotionFcn',@(~,~) deal());
end
He provided two reasons why the "impoly" function was not showing the animated lines:
================================
1. The "impoly" function uses the figure's "WindowButtonMotionFcn" callback to handle animating the line as you move the mouse around. Right now, the "WindowButtonUpFcn" gets rid of any existing callbacks in "WindoButtonMotionFcn". Although this allows for "hline" to stop moving when the mouse is released, it also removes the "impoly" function's callback as you release the mouse.
2. The "CurrentPoint" property is used to determine where the mouse is located in the axes data space. However, this property is only ever updated on mouse clicks or when the figure's WindowButtonMotionFcn is not empty. Because of this, the animation for "impoly" will work only if "WindowButtonMotionFcn" is not empty. Setting the "WindowButtonMotionFcn" to something that is not empty but still doesn't really do anything (e.g. deal()) could resolve this issue.
To deal with these issues, the following work around will work if using "drawpolygon" as opposed to "impoly".
The modified code avoids setting an empty callback in WindowButtonMotionFcn during drawing.
================================
The solution does work for drawpolygon and drawpolyline. It still does not work for impoly. But I should probably update my application anyway... :-)
Mega thanks to JK at Matlab Support for the solution!