Plotting Hourly,Daily,Monthly Energy Consumption
6 次查看(过去 30 天)
显示 更早的评论
I have a data file contianing the time and energy consumption for a small office. the data was collected every hour for a year.
firstly i would like to caterozige the data into days and months so i can sum thier energy consumptions
- how would find catorgize the data in such ways
I am unfamilar with datetime functions, ive tried to use this funtion in multiple ways unsucessfully. I could resort to using the Time coloumn in the data file to catogize the data but ive been recommneded datetime functions for the simplcity.
-for hourly, my coded attempt looked like the following:
Electricity=xlsread(['Book1','Sheet1','B:B');]
t=[datetime(2018,1,1,1):datetime(2018,end,end,end)].'; % build a sample dataset...
tt=timetable(t,randn(size(t))+10); % just some data to go with the dates
plot(tt.t,tt.Electricity)
secondly I want to appropiately label the data so that each point on the x axis corresponds to the hour,day,month.
- how would one label these points accordining for each plot
I am fiamilar with xlabel but that is just for the axis as a whole. Ive tried using timetables and datetick but these functions are confusing. Ive found some resources on simular questions. One i thought was helpful is found at: https://www.mathworks.com/support/search.html/answers/447221-plotting-timetable-data-how-to-add-x-axis-ticks-labels-and-gridlines-on-1st-and-15th-day-of-each-m.html?fq[]=asset_type_name:answer&fq[]=category:support/axis-labe1116&page=1
but this resources using functions that are hard for me to comprehend and follow in logic.
any help understanding these functions or suggestions for either question are greatly aprecciated.
thank you.
1 个评论
Cris LaPierre
2022-1-31
You forgot to attach your spreadsheet. You can use the paperclip icon to do that.
回答(1 个)
Cris LaPierre
2022-2-1
编辑:Cris LaPierre
2022-2-1
First, the first column of your table contains date and time. The easiest thing to do is to read that in along with the rest of the data in your spreadsheet. It is missing the year, so I add that to the datetimes after loading.
One other note. MATLAB does not support the 24:00:00, so those times get read in a NaT (not a time). In MATLAB, 24:00:00 would be inputted as 0:00:00 of the next day. All that means is I had to do a little correcting to replace the NaTs with the correct time as MATLAB expects it.
opts = detectImportOptions("Book1.xlsx");
opts = setvartype(opts,"Time","datetime");
opts = setvaropts(opts,"Time","InputFormat","MM/dd HH:mm:ss");
E = readtimetable('Book1.xlsx',opts);
% Add in the year
E.Time.Year = 2018;
% fix the NaTs by finding the time just before and adding 1 hour to it.
ind = 1:height(E);
ind = ind(ismissing(E.Time));
E.Time(ind) = E.Time(ind-1) + hours(1)
Since the data is already hourly, you could just plot the data to view that data:
plot(E.Time,E.Electricity)
With your data in a timetable, you can use the retime function to get daily, yearly, and monthly summaries fairly easily (see here). You can specify how you want to combine the data. Below I sum to get total electric consumption. You can learn more about how to specify other methods here.
% weekly sum
figure
Ewk = retime(E,'weekly','sum')
plot(Ewk.Time,Ewk.Electricity)
At this point, it would be very easy to also visualize Gas consumption. As an example, here is the code for visualizing the mean hourly consumption of gas each month.
% Monthly hourly mean
figure
Em = retime(E,'Monthly','mean');
plot(Em.Time,Em.Gas)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!