How do you plot data bi-monthly for the course of a year?

3 次查看(过去 30 天)
I have a data for a year where I need two plots for each month covering the 1st-15th and 16th-end of month, i.e. bi-monthly plots of this data.
The dataset isn't entirely complete and at some points there is no data for the 1st/15th/16th (as well as other days) of a month.
The data is currently in a timetable with time/date in datetime format 'dd/MM/yyyy HH:mm:ss'.
Is there a way of looping through the data to produce these bi-monthly plots?
My first instinct was to use Timerange, but cannot work out if there is a way to define 'half a month' using this function and plotting every 2 weeks or 15 days soon gets out of phase.
Below is a basic example of what the data looks like (I have added ... just to imply data continues during that time period)
date data
01/01/2018 00:00:00 344.98
01/01/2018 00:30:00 304.13
01/01/2018 01:00:00 355.74
...
13/01/2018 09:30:00 554.13
18/01/2018 10:00:00 344.77
19/01/2018 10:00:00 346.47
27/01/2018 00:30:00 304.13
02/02/2018 01:00:00 357.83
02/02/2018 01:30:00 367.87
...
31/12/2018 22:30:00 634.63
31/12/2018 23:00:00 400.53
31/12/2018 23:30:00 621.43
  1 个评论
Dom Smith
Dom Smith 2019-4-5
Perhaps the easiest solution will be to adjust the plot limits
with xlim(datetime(2018,[1 1],[1 15]))
and xlim(datetime(2018,[1 16],[1 30])) and so on (adjusting 30 depending on the number of days in a month).
But I am still not sure how I would write this into a loop.

请先登录,再进行评论。

采纳的回答

A. Sawas
A. Sawas 2019-4-6
You may use the function datetime to set the lower and upper dates of each bi-monthly period. Then using find to selectively choose the data points between them.
y = 2018;
for m=1:12
t1 = datetime(y,m,01);
t2 = datetime(y,m,16);
i = find(DataT.date => t1 && DataT.date < t2);
% Plot the first half of the month
plot(DataT.date(i), DataT.data(i));
t1 = t2;
t2 = datetime(y,m+1,1); % when m=12 the returned date is the first day of the next year
i = find(DataT.date => t1 && DataT.date < t2);
% Plot the second half of the month
plot(DataT.date(i), DataT.data(i));
end
  3 个评论
Carol pacheco
Carol pacheco 2021-9-28
and for cases of wanting to separate a large dataset? where the number of weeks are not exact (23 weeks and 3 days for example ?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by