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