MATLAB: Line Rendering Problem

lineplotrendering

I have a set of numbers that, when plotted, causes the linestyle to be displayed incorrectly.
The code below gives my dataset and plots them in subplot 1, the second subplot is an identical line (same max,min and number of datapoints in x and y), but with the datapoints evenly spaced along the line.
On my system (Matlab 7.11.0.584 (R2010b) running on Mac OS X 10.7.1) the line in the first plot is not displayed correctly, while it is in the second. Mysterious!
Any ideas?
Here's a picture of what I get… https://files.me.com/oscarbranson/siin06
% My data set
subplot(2,1,1)
x1=[4.98270000000000,4.95770000000000,4.94240000000000,4.95240000000000,4.93420000000000,4.98760000000000,4.98550000000000,4.95800000000000,4.96170000000000,4.94190000000000,4.98210000000000,4.98610000000000,4.95470000000000,4.96070000000000,4.93870000000000,4.98770000000000,4.98130000000000,4.98130000000000,4.98140000000000,4.98140000000000,4.98160000000000,4.98160000000000,4.98130000000000,4.98130000000000,4.98150000000000,4.98150000000000,4.98080000000000;];
y1=[17.0600972366516,16.9321690873624,16.8538770599974,16.9050483197131,16.8119166270306,17.0851711539123,17.0744251893720,16.9337042251539,16.9526375912487,16.8513184970117,17.0570269610687,17.0774954649550,16.9168177094477,16.9475204652771,16.8349436939026,17.0856828665095,17.0529332602914,17.0529332602914,17.0534449728886,17.0534449728886,17.0544683980829,17.0544683980829,17.0529332602914,17.0529332602914,17.0539566854857,17.0539566854857,17.0503746973056;];
plot(x1,y1,'r--')
% A generated line with the same number of data points and the same x and y
% limits.
subplot(2,1,2)
x2=min(x1):((max(x1)-min(x1))/(length(x1)-1)):max(x1);
y2=min(y1):((max(y1)-min(y1))/(length(y1)-1)):max(y1);
plot(x2,y2,'r--')

Best Answer

Your x1 are not sorted:
[x1,idx] = sort(x1);
plot(x1,y1(idx),'r--')
What happens is that the coordinates trace a trajectory that goes back and forth and the dashed sequence of the run in one direction can be overwritten by the a change in direction.