Question: [MATLAB GUI]When I draw a line on the axis, it shows four lines at the same time. Like this, when I draw the purple line.

[MATLAB GUI]When I draw a line on the axis, it shows four lines at the same time.

Like this, when I draw the purple line. It shows the purple line and three blue lines. How to modify this code to achieve the effect I want

Color is not important. I just want to know how to draw a line to show four lines. Code is here.

function [lineobj,xs,ys] = freehanddraw(varargin)

% [LINEOBJ,XS,YS] = FREEHANDDRAW(ax_handle,line_options)

axdef = 0;

if nargin ~= 0 && ishandle(varargin{1})

try

axes(varargin{1});

axdef = 1;

catch

error('If the initial input argument is a handle, it must be to a valid axis.');

end

end

%Get current figure and axis parameters

oldvals = get(gcf);

oldhold = ishold(gca);

hold on;

set(gcf,'Pointer','crosshair','doublebuffer','on');

%Get the initial point

[xs,ys] = ginput(1);

%Create and store line object

if axdef

lineobj = line(xs,ys,'tag','tmpregsel',varargin{2:end});

else

lineobj = line(xs,ys,'tag','tmpregsel',varargin{:});

end

setappdata(gcf,'lineobj',lineobj);

%Modify wbmf of current figure to update lineobject on mouse motion

set(gcf,'windowbuttonmotionfcn',@wbmfcn);

set(gcf,'windowbuttondownfcn',@wbdfcn);

%Wait for right-click or double-click

while ~strcmp(get(gcf,'SelectionType'),'alt') && ~strcmp(get(gcf,'SelectionType'),'open')

drawnow;

end

%Extract xy data from line object for return in output variables

%(Also retrievable from first output argument)

if nargout > 1

xs = get(getappdata(gcf,'lineobj'),'xdata')';

end

if nargout > 2

ys = get(getappdata(gcf,'lineobj'),'ydata')';

end

%Clear temporary variables from base workspace

evalin('caller','clear tmpx tmpy tmpz done gca lineobj');

%Reset figure parameters

set(gcf,'Pointer',oldvals.Pointer,...

'windowbuttonmotionfcn',oldvals.WindowButtonMotionFcn,...

'windowbuttondownfcn',oldvals.WindowButtonDownFcn,...

'doublebuffer',oldvals.DoubleBuffer);

%Reset hold value of the axis

if ~oldhold, hold off; end

function wbmfcn(varargin)

lineobj = getappdata(gcf,'lineobj');

if strcmp(get(gcf,'selectiontype'),'normal');

tmpx = get(lineobj,'xdata');

tmpy = get(lineobj,'ydata');

a=get(gca,'currentpoint');

set(lineobj,'xdata',[tmpx,a(1,1)],'ydata',[tmpy,a(1,2)]);

drawnow;

else

setappdata(gcf,'lineobj',lineobj);

end

function wbdfcn(varargin)

lineobj = getappdata(gcf,'lineobj');

if strcmp(get(gcf,'selectiontype'),'open')

tmpx = get(lineobj,'xdata');

tmpy = get(lineobj,'ydata');

a=get(gca,'currentpoint');

set(lineobj,'xdata',[tmpx,tmpx(1)],'ydata',[tmpy,tmpy(1)]);

setappdata(gcf,'lineobj',lineobj);

drawnow;

end

return

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!