I am trying to run the following function so that I can plot some points connected by a line, but the lines are not displaying in the color I want (white). I tried other colors as well, but the lines still won't show. I don't understand why. Can someone help?
function draw_constellations
figure
hold on
axis([0,100,0,100])
grid off
set(gcf,'color','k')
set(gca,'color','k')
axis square
while 1
[x,y,button] = ginput(1);
switch button
case 1 % left mouse
p = plot(x,y,'w-*');
case {'q','Q'} % on keyboard
break;
end
end
It is because x
and y
you used to plot are scalers. To plot lines, you will need to store all the x
and y
you get from ginput
in vectors and plot the vectors:
[x,y,button] = ginput(1);
switch button
case 1 % left mouse
xs = [xs x];
ys = [ys y];
p = plot(xs,ys,'w-*');
% more code to go
end
However, if xs
and ys
are plotted everytime a new point is entered, you will have lines overlapping. To avoid this, we only plot the first point and update the p.XData
and p.YData
for new points:
if isempty(p)
p = plot(x,y,'w-*');
else
p.XData = xs;
p.YData = ys;
Full code:
figure
hold on
axis([0,100,0,100])
grid off
set(gcf,'color','k')
set(gca,'color','k')
axis square
xs = [];
ys = [];
p = [];
while 1
[x,y,button] = ginput(1);
switch button
case 1 % left mouse
xs = [xs x];
ys = [ys y];
if isempty(p)
p = plot(x,y,'w-*');
else
p.XData = xs;
p.YData = ys;
end
case {'q','Q'} % on keyboard
break;
end
end
Result: