Skip certain times in datetime plot

I am trying to plot timeseries data using the MATLAB R2014b "datetime" method to enable nice features such as auto-zooming in to timestamps, etc. The following is an example:
dataToPlot =
'21/01/2015 09:00:00' '1'
'21/01/2015 10:00:00' '2'
'21/01/2015 11:00:00' '3'
'21/01/2015 12:00:00' '4'
'22/01/2015 09:00:00' '5'
'22/01/2015 10:00:00' '6'
'22/01/2015 11:00:00' '7'
'22/01/2015 12:00:00' '8'
So I would use "datetime" on the first column, and plot it against the second column. The problem is, the data on Jan 21 is from 9am - 12pm, the next data point is on Jan 22 at 9am, but "datetime" will fill a large portion of the central part of the graph will interpolated values between data '4' and '5'. Is there a way to avoid this, so that I can still use datetime but restrict the plot to specific periods of interest?
Thanks, Hamad

回答(1 个)

If you want to plot all of the data, but to not connect the line between some values, you could inject a NaN in your data where you want the line to break.
Here's an example of how to do it with brute force:
% Create your data
t = [datetime(2015,1,21,[9:12],0,0) datetime(2015,1,22,[9:12],0,0)]
d = 1:8;
% Inject the NaN into d. and put something into t. Doesn't matter what,
% since it won't plot.
d = [d(1:4) NaN d(5:end)];
t = [t(1:4) t(end) t(5:end)];
% Plot it
plot(t,d)

3 个评论

Thank you very much Michelle. We're almost there, but what I am really after is to not plot anything at all in the gap period. Your example indeed removes the line between data points 4 and 5, but the x-axis still contains times between 21st at 12pm and 22nd at 9am, therefore a large "blank".
The ideal solution is one in which the x-axis contains a "break", hence no gap. One way I can do this is to play around with XTick and XTickLabel, but ideally I would like to do it with datetime plot because datetime has a nice feature that relabels the x-axis ticks when you zoom in/out.
Thanks, Hamad
Dear Hamad, did you find a way to do this? I want to do exactly the same thing as you want.
However, this might not be compatible with datetime plots -- or at least it probably is not compatible with the automatic label changes upon zooming.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Line Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by