Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
Dom Smith
Dom Smith am 5 Apr. 2019
Kommentiert: A. Sawas am 28 Sep. 2021
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 Kommentar
Dom Smith
Dom Smith am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

A. Sawas
A. Sawas am 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
  3 Kommentare
Carol pacheco
Carol pacheco am 28 Sep. 2021
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 ?
A. Sawas
A. Sawas am 28 Sep. 2021
You can handle the fractions in a separate block of code

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by