MATLAB: How to retrieve the location of the points plotted ordered

arrayMATLAB and Simulink Student Suiteorderplot

I have two vectors x and y which are not sorted. However, when I plot(x,y), it results in the expected plot. Here is an example of the arrays and the plot. As you can see, I would like to have the vectors ordered starting from x(1) = 0, y(1) = 0 and then following the order of the dots as if it was a continuous line. I cannot figure it out how to automatize this process since I have quite a lot arrays/graphs to look at and it is not worth it to do it manually.
x = [1.99802555286103e-05 2.11219844159594e-05 2.28345777469832e-05 2.39763066343323e-05 2.54034677435188e-05 2.59743321871934e-05 2.65451966308679e-05 2.74014932963798e-05 2.85432221837290e-05 2.85432221837290e-05 2.85432221837290e-05 2.85432221837290e-05 2.79723577400544e-05 2.76869255182171e-05 2.65451966308679e-05 2.54034677435188e-05 2.42617388561696e-05 2.28345777469832e-05 2.11219844159594e-05 1.91239588630984e-05 1.71259333102374e-05 1.59842044228882e-05 1.39861788700272e-05 1.19881533171662e-05 9.70469554246784e-06 7.42123776776952e-06 5.70864443674578e-06 3.99605110572207e-06 1.99802555286102e-06 0];
y = [-3.68207566170104e-05 -3.48227310641493e-05 -3.33955699549629e-05 -3.13975444021019e-05 -2.96849510710781e-05 -2.74014932963798e-05 -2.54034677435188e-05 -2.34054421906578e-05 -2.11219844159595e-05 -1.88385266412611e-05 -1.62696366447255e-05 -1.39861788700272e-05 -1.11318566516543e-05 -9.13383109879327e-06 -7.99210221144412e-06 -6.27950888042038e-06 -5.13777999307122e-06 -3.71061888388478e-06 -3.13975444021020e-06 -3.13975444021020e-06 -2.85432221837291e-06 -2.85432221837291e-06 -2.56888999653562e-06 -2.56888999653562e-06 -2.56888999653562e-06 -2.28345777469833e-06 -2.28345777469833e-06 -1.99802555286104e-06 -1.42716110918645e-06 0];

Best Answer

The easiest way to do that is to use the flip function (or fliplr or flipud, depending on what you want to do).
Try this:
flipx = flip(x);
flipy = flip(y);
figure
plot(flipx, flipy)
grid
dx = gradient(flipx); % Optional



dy = gradient(flipy); % Optional
figure % Optional
quiver(flipx, flipy, dx,dy) % Optional
grid
The quiver plot just demonstrates that the flip operation does what you want it to do. It is not necesary for the code.