Plotting hourly data for every month for 'x' number of years
1 次查看(过去 30 天)
显示 更早的评论
Hello Matlab Community,
I am trying to group the data into monthly bins for 'x' number of years. End goal is to plot the data for every month as shown in attahed screenshot.
Problem: My Date is combination of date and time. I have tried isbetween , find but I am not able to make it work. Sometimes, it gives only 30 values as I specify the date and time. Sometimes, it takes 17280 values. I want to create matrix of data for each month so that they can be plotted. I am attaching the example excel sheet and one example of plot. (Nov 2004)
This is just example but formatting is not important as long as data is separated into every month of each year. I am good with that.
Year 2004
Jan Feb Mar Apr May June July Aug Sept Oct Nov Dec
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
Year 2005
Jan Feb Mar Apr May June July Aug Sept Oct Nov Dec
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
and so on till year 2022
采纳的回答
dpb
2022-7-19
编辑:dpb
2022-7-19
tData=readtable('test.xlsx');
tData=addvars(tData,year(tData.Date_Time),month(tData.Date_Time),'Before','WaterLevel','NewVariableNames',{'Year','Month'});
rowfun(@ploteach,tData,'GroupingVariables',{'Year','Month'},'InputVariables',{'Date_Time','WaterLevel','WaterLevel2'})
with
function hL=ploteach(t,v1,v2)
figure
hL=plot(t,[v1 v2]);
end
ran but crashed MATLAB low-level graphics after creating all 223 figures in a flash -- but didn't successfully draw any of them on screen. Hadn't ever done that before with any more than 2-3 groups, but thought would give it a try.
So, create your grouping variables and run a conventional loop --- or use isbetween with updating the month and year -- in this case a double loop would be easier to code, probably.
>> ix=isbetween(tData.Date_Time,datetime(2004,1,1),datetime(2004,1,31,23,0,0));
>> sum(ix)
ans =
744
>> ix=isbetween(tData.Date_Time,'01-Jan-2004','31-Jan-2004 23:00');
>> sum(ix)
ans =
744
>>
either form works; it's probably easier to code the second with a variable for year and month and then eomday() for the second limit...
uy=unique(year(tData.Date_Time));
for y=1:numel(uy)
for m=1:12
ix=isbetween(tData.Date_Time,datetime(uy(y),m,1),datetime(uy(y),m,eomday(uy(y),m),23,0,0));
figure
plot(tData.Date_Time(ix),[tData.WaterLevel(ix) tData.WaterLevel2(ix)])
% fixup plot here with labels, etc., etc., ...
end
This again will create a bunch of figures -- may still run into issues unless do something like make a 4x3 or 6x2 suplot and put each year in a single figure....
4 个评论
dpb
2022-7-20
编辑:dpb
2022-7-20
Oh, yeah, problem with "air code" -- one tends to forget the screen representation of a datenum isn't really the string it looks like -- I intended that to have been
outname=['MonthlyPlot' char(datetime(uy(y),m,1),'Format',"yyyy-MM"))'.jpg'];
so that the files will sort in chronological order.
更多回答(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!