Plotting hourly data for every month for 'x' number of years

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

 Risposta accettata

dpb
dpb il 19 Lug 2022
Modificato: dpb il 19 Lug 2022
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 Commenti

Harjas
Harjas il 20 Lug 2022
Modificato: Harjas il 20 Lug 2022
Thanks a lot. I was able to add xlabel, ylabel , title and legend as well to individual plots. But, I am having two problem with saving the plots:
  1. Using this code, it is only saving the last plot but none of the other plots.
  2. How to include the filename which includes the month and year for that plot.
saveas(gcf,['Monthly Plot .jpg']);
tData=readtable('test.xlsx');
tData=addvars(tData,year(tData.Date_Time),month(tData.Date_Time),'Before','WaterLevel','NewVariableNames',{'Year','Month'});
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)])
xlabel('Date')
ylabel('Water Level (m)')
legend({'(u/s)','(d/s)'},'Location','best')
saveas(gcf,['Monthly Plot .jpg']);
% fixup plot here with labels, etc., etc., ...
end
end
  1. Using this code, it is only saving the last plot but none of the other plots.
  2. How to include the filename which includes the month and year for that plot.
  1. Au contraire -- it's saving them all, just overwriting the same file since you didn't
  2. include the filename which includes the month and year for that plot or any other differentiation.
So, create an output file name --
...
for m=1:12
...
outname=['MonthlyPlog' datetime(uy(y),m,1),'Format',"yyyy-MM") '.jpg'];
saveas(gcf,outname);
end
Possibly you may want to close the figure after writing it out -- that would eliminate the creation of the some 220+ figures all open at once.
Sorry to bug you.I have just started learning MATLAB. I modified your code a bit as it was giving me error that 'MonthlyPlog' cannot be converted to datetime.
Code Used:
outname=['MonthlyPlot ',datestr(datetime(uy(y),m,1)), '.jpg'];
saveas(gcf,outname);
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.

Accedi per commentare.

Più risposte (0)

Categorie

Prodotti

Release

R2018a

Richiesto:

il 19 Lug 2022

Modificato:

dpb
il 20 Lug 2022

Community Treasure Hunt

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

Start Hunting!

Translated by