MATLAB: Distances along a plotted line

2ddistancedistance along curvedistance along linelengthlinspacemarkerplot

I have x and y matrices with y representing elevations at points x.
For instance
x = [0 5 10]
y = [2 4 1]
plot(x,y) gives me a cross section of elevations.
I want to mark various distances along the plotted line. For instance, place a marker every 1m along the line, or put individual markers at specific distances along the line.
How can I do this?
Thanks!

Best Answer

marker_dist = 1;
x = [0 5 10];
y = [2 4 1];
dist_from_start = cumsum( [0, sqrt((x(2:end)-x(1:end-1)).^2 + (y(2:end)-y(1:end-1)).^2)] );
marker_locs = marker_dist : marker_dist : dist_from_start(end); %replace with specific distances if desired
marker_indices = interp1( dist_from_start, 1 : length(dist_from_start), marker_locs);
marker_base_pos = floor(marker_indices);
weight_second = marker_indices - marker_base_pos;
marker_x = x(marker_base_pos) .* (1-weight_second) + x(marker_base_pos+1) .* weight_second;
marker_y = y(marker_base_pos) .* (1-weight_second) + y(marker_base_pos+1) .* weight_second;
plot(x, y);
hold on;
plot(marker_x, marker_y, 'r+');
hold off