How to plot time data at different/random time intervals?

Like the title says I'd like to plot some vehicle data, which occurs every 5s, with timestamps of different non consecutive dates (different days). The problem that I have,is that there's a kind of a "gap" in the plot. I'm guessing because when the day changes there's a sustantial increment in the data in time (left picture). I'd like to have a similar outcome as in excel (right picture).
I appreciate your help.
MATLAB PLOT: EXCEL PLOT:
A1time:
'16-Oct-2019 09:22:05'
'16-Oct-2019 09:22:10'
'16-Oct-2019 09:22:15'
'16-Oct-2019 09:22:20'
'16-Oct-2019 09:22:25'
'22-Oct-2019 08:41:45'
'22-Oct-2019 08:41:50'
'22-Oct-2019 08:41:55'
'22-Oct-2019 08:42:00'
'22-Oct-2019 08:42:05'
A1qVeh:
318.819
358.549
480.068
692.451
417.788
351.383
0
232.397
1114.91
1520.94
so far I've tried plotting directly the datetime variable and used datenum & datetick.
figure()
plot (A1time,A1qVeh)
B1DateTime = A1time-datenum('16-Oct-2019','dd-mmm-yy')
figure()
plot(B1DateTime,A1qVeh)
dateFormat = 'HH:MM:SS';
datetick('x',dateFormat)

2 Comments

Attach a .mat file with representative data for to make it easier for folks...
If you use datetime on the axis, then indeed points will be plotted at their real respective times.
The datenum trick against ordinal position works but is klunkier than could be owing to datetick being rather difficult.
I'd guess the best compromise is to plot against the serial order of the data, but then use the associated datetime variable as tick labels. You can find() where date changes and pick an interval from those points to write a reasonable number of labels but still pick out the beginning of data sections.
That's a great advice to attach the .mat file, will do next time.

Sign in to comment.

 Accepted Answer

While not sophisticated to handle general case to isolate the day start locations, the general idea is
A1time=datetime(A1time);
hL=plot(A1qVeh);
xticklabels(string(A1time))
hAx=gca;
hAx.XAxis.TickLabelRotation=90;
hAx.XAxis.FontSize=8;
leaves one with

3 Comments

That worked perfect! Thanks a lot dpb.
Yes, plotting against a datetime assumes you want a continuous timeline on the axis. The best way to do what you want is, as dpb shows, to plot against indices, and then manually label those ticks with your timestamps.
I've wished for (and put in enhancement requests as long as 25-30 years ago) to allow "broken" axes to accomodate gaps when wanted/needed...

Sign in to comment.

More Answers (0)

Products

Release

R2020a

Asked:

on 5 Jul 2021

Commented:

dpb
on 29 Jul 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!