dates missing in time series - how to remove discontinuity

4 views (last 30 days)
I am plotting some daily time series data and there are about 60 contiguous days missing from the middle of the data set. When i plot, I am getting this ugly discontinuity where there are missing days and I can't, for the life of me, figure out how to close this "gap." I'm using Matlab 2012b on Mac OS X. Thanks in advance.
my code:
figure
plot(dates,Y,'linewidth',1)
set(gca,'XTick',dates)
datetick('x','mm-yyyy')
xlabel('Date')
  2 Comments
Iain
Iain on 16 May 2013
I suspect dates has some NaN in it. If you:
t = isnan(dates); dates(t) = []; Y(t) = [];
Then you ought to get the plot without the gap.
It may make more sense to interpolate to get the missing dates (if you have valid Y data)
John
John on 17 May 2013
There are no NANs in the data, I've already removed them. The issue is that dates are on the x-axis and the missing date range just gets skipped and there is a gap with a straight line going from the last data point before the gap to the first data point after the gap.
I've tried both having NaNs and no NaNs, same result. Any ideas?

Sign in to comment.

Accepted Answer

José-Luis
José-Luis on 17 May 2013
What do you dates look like? If it is something like 800415 (ddmmyy) then there will be a gap at the end of every month and a larger one at the end of every year. If that is the case an idea is to use Matlab day number.
doc datenum
In the example above:
serial_date = datenum(800415,'ddmmyy');
The function works with arrays and for plenty of date formats. You could then
plot(serial_data,your_data);
  2 Comments
John
John on 17 May 2013
Yes, I am using datenums. just before the code posted in my question above is a line that is:
dates = datenum(num2str(dates),'yyyymmdd');
I still get the gap. It's basically like matlab knows there are dates missing and just leaves the gap to preserve the distance between xticks. I'm sure things would look strange if you had daily data and it plotted the xaxis like:
| | | | |
2005 2006 2007 2008 2009
instead of:
| | | | |
2005 2006 2007 2008 2009
But the consequence is gaps in the plotted data for the latter method (which is what I'm seeing and can't seem to get the first method).
José-Luis
José-Luis on 20 May 2013
Not entirely sure what you want to achieve. But if you want to get rid of the gap, you could use a continuous date series and fill in with NaN's where there are no data.
Sort of like this.

Sign in to comment.

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!