Need help modifying a piece of code that allows the user to draw lines

1 view (last 30 days)
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 ([-10 10 -10 10])
%open the file to save the coordinates into
fileID = fopen('coord.txt','w');
function wbdcb(src,evnt)
if strcmp(get(src,'SelectionType'),'normal')
[x,y,str] = disp_point(ah);
%write the coordinates into the file
A=[x,y];
fprintf(fileID,'%6.2f %6.2f\n',A);
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);
%write the coordinates into the file
A=[x,y];
fprintf(fileID,'%6.2f %6.2f\n',A);
text(x,y,str,'VerticalAlignment','bottom');drawnow
fclose(fileID);
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
I need to modify this code so that the first line starts from the origin(0,0) and there onwards the user can click on the graph to draw lines. This is a recycled code that I found online so I am not entirely sure how it works. Any help would be much appreciated.

Accepted Answer

Purushottama Rao
Purushottama Rao on 18 Aug 2015
If you want to see {0,0} before the mouse click, use this code
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
%
clc
figure('WindowButtonDownFcn',@wbdcb)
k=1;
ah = axes('DrawMode','fast');
axis ([-10 10 -10 10])%open the file to save the coordinates into
fileID = fopen('coord.txt','w');
hl = line('XData',0,'YData',0,'Marker','.');
str=['(',num2str(0),num2str(0),')'];
text(0,0,str,'VerticalAlignment','bottom');drawnow
function wbdcb(src,evnt)
if strcmp(get(src,'SelectionType'),'normal')
[x,y,str] = disp_point(ah); %write the coordinates into the file
A=[x,y];
fprintf(fileID,'%6.2f %6.2f\n',A); 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); %write the coordinates into the file
A=[x,y];
fprintf(fileID,'%6.2f %6.2f\n',A); text(x,y,str,'VerticalAlignment','bottom');drawnow
fclose(fileID);
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)
if k==1
x=0;y=0;
k=0;
else
cp = get(ah,'CurrentPoint');
x = cp(1,1);y = cp(1,2);
end
str = ['(',num2str(x,'%0.3g'),', ',num2str(y,'%0.3g'),')'];
end
end
  1 Comment
Ashfaq Ahamed
Ashfaq Ahamed on 20 Aug 2015
Thanks a lot Purushottama Rao, but this solves just one part of the problem. It displays the origin (0,0) but I want the first click to be at this point as well but without the user having to click on it manually. Any feedback would be much appreciated

Sign in to comment.

More Answers (1)

Purushottama Rao
Purushottama Rao on 18 Aug 2015
Edited: Purushottama Rao on 18 Aug 2015
Assign a flag for the first click and reset it afterwards. Use the flag to set the coordinates to {0,0}. Hope this is what you are looking for
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
%
clc
figure('WindowButtonDownFcn',@wbdcb)
k=1; % Flag for origin
ah = axes('DrawMode','fast');
axis ([-10 10 -10 10])%open the file to save the coordinates into
fileID = fopen('coord.txt','w');
function wbdcb(src,evnt)
if strcmp(get(src,'SelectionType'),'normal')
[x,y,str] = disp_point(ah); %write the coordinates into the file
A=[x,y];
fprintf(fileID,'%6.2f %6.2f\n',A); 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); %write the coordinates into the file
A=[x,y];
fprintf(fileID,'%6.2f %6.2f\n',A); text(x,y,str,'VerticalAlignment','bottom');drawnow
fclose(fileID);
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)
if k==1
x=0;y=0;
k=0; % Reset the flag for origin
else
cp = get(ah,'CurrentPoint');
x = cp(1,1);y = cp(1,2);
end
str = ['(',num2str(x,'%0.3g'),', ',num2str(y,'%0.3g'),')'];
end
end
if true
% code
end

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!