- 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 (최근 30일)
이전 댓글 표시
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
채택된 답변
Adam Danz
2020년 12월 14일
편집: Adam Danz
2020년 12월 14일
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
Adam Danz
2020년 12월 14일
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.
추가 답변 (1개)
Cris LaPierre
2020년 12월 14일
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
Adam Danz
2020년 12월 14일
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
2020년 12월 14일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Discrete Data Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

