- The timetable is broken apart by year using arrayfun and each subtable is stored in a cell array TTbyYear.
- The dates of each subtable are shifted to 1952, preserving the month and day. Note that 1952 is a leapyear. It's important that all dates share the same year and since some years in your data may be leapyears, the shared year should be a leapyear.
- The subtables are joined back together using outerjoin where each year has its own column and the variable names in the new table TTy are the original years.
- The new table TTy is sent to stackedplot().
Timetable Variable grouped by year and stackedplot
10 views (last 30 days)
Show older comments
I have the following unstacked timetable that I would like to plot with the stackedplot function.
The stackedplot should have the Day/Month as the X-Axis and it should be "stacked" or grouped by the year.
Should the grouping be done by some timetable function or should I just loop through the timetable with the required conditions? Thanks!
Datum DailyAvgTemp
__________ ____________
01.01.1956 -6.8
02.01.1956 -3.7
03.01.1956 -4.5
04.01.1956 -5.2
05.01.1956 0
06.01.1956 -3.8
...
05.11.2018 -2
06.11.2018 -2.8
0 Comments
Accepted Answer
Adam Danz
on 14 Dec 2020
Edited: Adam Danz
on 14 Dec 2020
Steps taken to break apart a timetable by years and plot in stackedplot
Demo
% Create demo timetable
TT = timetable(round(rand(3652,1)*100)/10-5,...
'StartTime',datetime(1956,1,1,'Format','dd.MM.yyyy'),...
'TimeStep',days(1),...
'VariableNames',{'DailyAvgTemp'});
TT.Properties.DimensionNames{1} = 'Datum';
head(TT) % show sample
% Break apart timetable by year
TTyears = year(TT.Datum);
unqYears = unique(TTyears);
TTbyYear = arrayfun(@(y){TT(TTyears==y,:)},unqYears);
% Join the tables back into a timetable with 1 column for each year
baseYear = 1956; % should be a leap year
TTy = timetable();
for i = 1:numel(TTbyYear)
% Change year to base year
TTbyYear{i}.Datum = datetime(baseYear,month(TTbyYear{i}.Datum),day(TTbyYear{i}.Datum));
% join tables
TTy = outerjoin(TTy, TTbyYear{i});
end
% Change variable names to years
TTy.Properties.VariableNames = compose('%d',unqYears);
% Change format of datetime values to hide the year
TTy.Datum.Format = 'dd.MM';
head(TTy) % Show sample
% Plot
stackedplot(TTy)
2 Comments
Adam Danz
on 14 Dec 2020
Glad I could help.
I just made a small change to the demo. The variable baseYear was not used and I hard-coded the year within the loop instead. That was fixed so that baseYear is used within the loop.
More Answers (1)
Cris LaPierre
on 14 Dec 2020
For stackedplot to work as you intend, you would have to make each year's data its own variable (column). There is no timetable function for that. Also note that stackedplot has a maximum of 25 variables (1 variable = 1 plot). It looks like you might have 63.
4 Comments
Adam Danz
on 14 Dec 2020
If you were set on using a stacked plot, you could break apart your data into 3-5 year segments and show multiple years in each axes.
Cris LaPierre
on 14 Dec 2020
Looks like I incorrectly assumed dates were mm-dd-yyyy, which is simpler to solve. If it's dd-mm-yyyy, it gets a little more challenging.
See Also
Categories
Find more on Discrete Data Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
