Adding a new line to graphic making it increment in position by 1 pixel each time, whilst preserving image and other lines.

1 view (last 30 days)
I want to manually change the location of the green line on the graphic below so that it aligns up with the nearest object along the red line (in this example, it will be about 2 pixels to the right)
So my aim is to use two pushbuttons (left and right) that once pressed will draw a magenta line on the graphic preserving the image and the other lines. As the user presses either left or right, only the newly added magenta line will be updated so either move to the right or left by one pixel. This will then allow me to count the actual shift i need to make sure the green line, at its centre is over a bright object.
%delete(hline1)
axes(handles.axes4)
img=getimage(handles.axes4)
%The starting location is the middle
r=size(img,1)
c=size(img,2)
rM=round(r/2)
cM=round(c/2)
hold on
hline1=plot([cM,cM],[0 r],'m--')
I have several issues. I don't know how to remove the hline I draw when I next press either of the buttons. i was looking for something along the lines of
ifexists(hline) then....
Also, I can't work out initially the magenta line starts at the centre of the image, and increments its position by 1 pixel every time the push button is pressed.
  1 Comment
Jason
Jason on 2 Nov 2015
I have tried this:
axes(handles.axes4);
img=getimage(handles.axes4);
%The starting location is the middle
r=size(img,1)
c=size(img,2)
rM=round(r/2)
cM=round(c/2)
hold on
if exist('hline1', 'var')
delete(hline1);
xx=xx+1
else
xx=cM
end
hline1=plot([xx,xx],[0 r],'m--');

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 2 Nov 2015
Don't delete the line and recreate it. Update the properties of the existing line, in particular its XData and YData.
% Create the line
h = plot(1:10, 1:10);
% Assuming release R2014b or later, update it
% For pre-R2014b, use GET and SET.
h.YData = h.YData + sqrt(1:10);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!