MATLAB Answers

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

7 views (last 30 days)
Dom Smith
Dom Smith on 5 Apr 2019
Commented: Dom Smith on 8 Apr 2019
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 Comment

Dom Smith
Dom Smith on 5 Apr 2019
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.

Sign in to comment.

Accepted Answer

A. Sawas
A. Sawas on 6 Apr 2019
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

  1 Comment

Dom Smith
Dom Smith on 8 Apr 2019
Thanks very much, with a bit of tweaking and correction that was the basis for a nice bit of code.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!