MATLAB: Write a function called draw_constellations that takes no input arguments and returns no output arguments. Instead, it obtains its input via the function ginput.

MATLABplot

Write a function called draw_constellations that takes no input arguments and returns no output arguments. Instead, it obtains its input via the function ginput. See the previous problem for an explanation of how ginput works. Like the function in the previous problem, this function allows the user to plot points by clicking in a figure whose horizontal and vertical ranges are set to be 0 to 100, but with this function the plot symbol '*' is used, and the points are joined by straight lines. Furthermore, the color of the plotted symbols and lines must be white, and before plotting begins, the function must use the following two commands to set the background to black: set(gcf,'color','k') and set(gca,'color','k'), and it must issue the command axis square. Then, the function must enter a while-loop to plot the first point and then plot subsequent points with lines joining each point to the previous point. These connected points represents a constellation (more precisely, a star pattern). If the userclicks the N-key, with or without the Shift key depressed, then a new set of connected points is begun—not connected to the previous connected pattern. Finally, when the user hits the Q-key (with or without Shift), the loop must end and the function must return. Figure 2.43 is an example of the result of two connected patterns showing the use of draw_constellations to draw the constellations Ursa Major and Ursa Minor.
The problem my code is giving that it is not printing line but only marker.
function constelation
figure
hold on
axis([0 100 0 100]);
set(gca,'color','k');
set(gcf,'color','k');
while 1
a = 1;
[x y button] = ginput(a);
switch button
case 1
plot(x,y,'r-');
case {'n','N'}
a = 2;
case { 'q','Q'}
break;
end
end

Best Answer

Create the line object prior to the while-loop using NaN values for the x and y data. Then update the x and y data within the loop.
h = plot(nan, nan, 'r-o'); % Set the line properties here, too
while 1
. . .
a = 1;
[x y button] = ginput(a);
set(h,'XData', [h.XData, x], 'YData', [h.YData, y]) % Update line coordinates
. . .
end
h.XData(1) = []; % remove initial NaN

h.YData(1) = []; % remove initial NaN