Daily mean through long time

22 views (last 30 days)
Nada
Nada on 21 Dec 2024 at 12:44
Commented: Star Strider about 18 hours ago
Hi All
The variable, the year, the month, the day, the hour, and the minutes are all in a data file.
i need to find a loop that can find out the variable's daily average.
Pay attention to the point that the variable has 24 values during the day. This means that it changes every hour.
Please help me create a loop so that I can find the average daily value for the month and year.

Accepted Answer

Star Strider
Star Strider about 10 hours ago
T1 = readtable('data.xls') % Import Data
T1 = 17280x6 table
YYYY mm ddd HH mint avs ____ __ ___ __ ____ _____ 2012 1 12 0 30 1.12 2012 1 12 1 30 2.12 2012 1 12 2 30 3.12 2012 1 12 3 30 4.12 2012 1 12 4 30 5.12 2012 1 12 5 30 6.12 2012 1 12 6 30 7.12 2012 1 12 7 30 8.12 2012 1 12 8 30 9.12 2012 1 12 9 30 10.12 2012 1 12 10 30 11.12 2012 1 12 11 30 12.12 2012 1 12 12 30 13.12 2012 1 12 13 30 14.12 2012 1 12 14 30 15.12 2012 1 12 15 30 16.12
DateTime = datetime([T1{:,1:5} zeros(size(T1,1),1)]); % Create ‘datetime’ Array From Available Data & Add ‘zeros’ Vector Of Seconds
T1 = removevars(T1, 1:5); % Remove Original Datee & Time Data
T1 = addvars(T1, DateTime, Before=1) % Add New ‘datetime’ Array
T1 = 17280x2 table
DateTime avs ____________________ _____ 12-Jan-2012 00:30:00 1.12 12-Jan-2012 01:30:00 2.12 12-Jan-2012 02:30:00 3.12 12-Jan-2012 03:30:00 4.12 12-Jan-2012 04:30:00 5.12 12-Jan-2012 05:30:00 6.12 12-Jan-2012 06:30:00 7.12 12-Jan-2012 07:30:00 8.12 12-Jan-2012 08:30:00 9.12 12-Jan-2012 09:30:00 10.12 12-Jan-2012 10:30:00 11.12 12-Jan-2012 11:30:00 12.12 12-Jan-2012 12:30:00 13.12 12-Jan-2012 13:30:00 14.12 12-Jan-2012 14:30:00 15.12 12-Jan-2012 15:30:00 16.12
TT1 = table2timetable(T1); % Converet To ‘timetable’
TT1 = retime(TT1, 'daily', 'mean') % Calculate & Show Results
TT1 = 720x1 timetable
DateTime avs ___________ ______ 12-Jan-2012 12.62 13-Jan-2012 36.62 14-Jan-2012 42.862 15-Jan-2012 25 16-Jan-2012 25 17-Jan-2012 25 18-Jan-2012 25 19-Jan-2012 25 20-Jan-2012 19 21-Jan-2012 1 22-Jan-2012 1 23-Jan-2012 1 24-Jan-2012 1 25-Jan-2012 1 26-Jan-2012 1 27-Jan-2012 1
[t1,t2] = bounds(T1.DateTime(T1.avs >= 5))
t1 = datetime
12-Jan-2012 04:30:00
t2 = datetime
20-Jan-2012 17:30:00
figure
plot(T1.DateTime, T1.avs, DisplayName='Original Data')
hold on
stairs(TT1.DateTime, TT1.avs, DisplayName='Daily Mean', LineWidth=2)
hold off
grid
xlim([t1 t2]+[-1 1]*days(3))
xlabel('Time')
ylabel('avs')
legend(Location='SW')
.
  2 Comments
Nada
Nada about 1 hour ago
Thank you for your assistance. It was very helpful in my instance.
Star Strider
Star Strider 20 minutes ago
As always, my pleasure!

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson about 10 hours ago

build a timetable() from the data. Use retime()

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!