
Generate Plot Based on Interval Data
7 views (last 30 days)
Show older comments
How would I generate a plot if I have data from intervals. I'm not sure if 'interval' is the right technical term.
What I mean is I have data like this
[210 320 430 250 110 ...] <-represents distance
[30 50 10 20 40...] <-represents speed limit
For the first 210 metres, the speed limit is 30km/h. For the subsequent 320m, the speed limit is 50km/h, and so on.
It would look like the blue line in this figure.

Is there a simple way to plot such data? Of course, I can set a loop and interpolate the speed at a distance interval of say, 1 metres. Then subsequently, use the plot(distance,speed) function. But would there be a more elegant way?
Subsequently, I would also want to modify the curve. It's impossible for a car to follow the blue speed curve as it requires infinite acceleration/deceleration. I would like to limit the speed curve to the red curve, by specifying a constant acceleration/deceleration rate of e.g. 1m/s^2.
Would there be a simple way to do this as well?
I've thought of a long method, which is to construct the red slope using y=mx+c. Then define which sections the y=mx+c is valid for, and lastly take the minimum of the blue lines and y=mx+c at every interval, say 1 metres. And this would be repeated for every red slope which exists.
0 Comments
Accepted Answer
jonas
on 9 Aug 2018
Edited: jonas
on 9 Aug 2018
%%Data
x=[0 210 320 430 250 110]; %NOTE EXTRA 0
x=[cumsum(x)];
y=[30 50 10 20 40 NaN]; %NOTE EXTRA NaN
figure;;hold on
axis([min(x) max(x)+120 0 100])
%%Change to stairstep
[xb,yb]=stairs(x,y)
plot(xb,yb)
%%Adjust x
dy=diff(yb)
SlowDown=find(dy<0);
SpeedUp=find(dy>0);
xb(SlowDown)=xb(SlowDown)-10;
xb(SpeedUp+1)=xb(SpeedUp+1)+10;
set(gca,'xtick',x(1:end-1))
plot(xb,yb,'r')
ytickformat('%g km/h')
xtickformat('%g m')
If you want a constant slope, then you just make the change (currently 10) proportional to dy.

7 Comments
jonas
on 10 Aug 2018
Edited: jonas
on 10 Aug 2018
I was so close so figured I'd finish it :)
You can try running the attached script, but you need the fileexchange function InterX ( link ). I have only tested it against a few data points but it looks ok. Figure is for constant acceleration (m/s^2) , but it is very easy to change.

More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!