Code 1
You're drawing a line with the x and y coordinates defined in the vectors with the plot(x,y) command. By default, it means plot(x,y,'-'); which means you draw a line (-).
Code 2
You're drawing individual points (no line) stored in the vectors since you're calling plot for each point (xx,yy)
You can duplicate the effect in Code 2 in Code 1, with the following change:
plot(x,y,'.');
This forces MATLAB to only plot the points and not the connecting line
If you want the points and the line,
plot(x,y,'-.');
For more details, check out the documentation of the plot command.
Sample Code:
%# If you know the number of elements, preallocate
%# else just initialize xx = []; and yy =[];
xx = zeros(100,1);
yy = zeros(100,1);
%# Get the data
for i = 1:length(xx)
xx(i) = getXSomehow();
yy(i) = getYSomehow();
end
%# Plot the data
plot(xx,yy);