Putting Timetables Together to Create One Large Table

Hi,
I have a code. And I want to put the columns of timetables sequentially.
For instance, in my code produces these timetables tt5, tt11, tt17, tt23, tt29, tt36, tt41, tt47, tt53, tt59. They represent the years 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, and 2019, respectively.
Following the last row of tt5, I want the first row of tt11 to begin. Then following the last row of tt11, I want the first row of tt17 to begin. In this way, there creates one large table, with the data running straight from 2010 thru 2019.
I would appreciate any help. Thank you.

6 Comments

  1. Don't build specific table variable names with numeric subscripts -- use array or cell array to be able to reference variables programmatically.
  2. Just build the table you have in mind from the git-go instead of making up all the intermediaries -- you have to read the various data to produce the various years of data anyway, just add to the one master table.
I know. But I'm getting the tables from this code:
%2010
tt0 = readtable('BackupByZipCode1.xlsx');
tt1=table2timetable(tt0)
tt1.ZIPCODE = categorical(tt1.ZIPCODE);
tt1.Dummy = ones(height(tt1),1);
tt2 = unstack(tt1,'Dummy','ZIPCODE');
tt2 = fillmissing(tt2,'Constant',0);
caldiff(tt1.Date([1 end]),'days')
t = datetime(2010,1,1):caldays(1):datetime(2010,12,31);
tt3 = retime(tt2,t,'FillWithConstant','Constant',0);
tt3.DoY = day(tt3.Date,'dayofyear');
tt4 = timetable2table(tt3,'ConvertRowTimes',false);
tt4 = varfun(@sum,tt4,'GroupingVariable','DoY');
tt3.MoY = month(tt3.Date);
tt3.DoM = day(tt3.Date);
tt3.WoY = week(tt3.Date);
tt4 = timetable2table(tt3,'ConvertRowTimes',false);
tt4 = varfun(@sum,tt4,'GroupingVariables',{'WoY' 'MoY' 'DoM'});
leapDayRows = (tt4.MoY==2) & (tt4.DoM==29);
tt4(leapDayRows,:) = [];
tt5 = varfun(@sum,tt4,'GroupingVariable','WoY')
%2011
tt6 = readtable('BackupByZipCode1.xlsx');
tt7=table2timetable(tt6)
tt7.ZIPCODE = categorical(tt7.ZIPCODE);
tt7.Dummy = ones(height(tt7),1);
tt8 = unstack(tt7,'Dummy','ZIPCODE');
tt8 = fillmissing(tt8,'Constant',0);
caldiff(tt7.Date([1 end]),'days')
t0 = datetime(2011,1,1):caldays(1):datetime(2011,12,31);
tt9 = retime(tt8,t0,'FillWithConstant','Constant',0);
tt9.DoY = day(tt9.Date,'dayofyear');
tt10 = timetable2table(tt9,'ConvertRowTimes',false);
tt10 = varfun(@sum,tt10,'GroupingVariable','DoY');
tt9.MoY = month(tt9.Date);
tt9.DoM = day(tt9.Date);
tt9.WoY = week(tt9.Date);
tt10 = timetable2table(tt9,'ConvertRowTimes',false);
tt10 = varfun(@sum,tt10,'GroupingVariables',{'WoY' 'MoY' 'DoM'});
leapDayRows = (tt10.MoY==2) & (tt10.DoM==29);
tt10(leapDayRows,:) = [];
tt11 = varfun(@sum,tt10,'GroupingVariable','WoY')
%2012
tt12 = readtable('BackupByZipCode1.xlsx');
tt13=table2timetable(tt12)
tt13.ZIPCODE = categorical(tt13.ZIPCODE);
tt13.Dummy = ones(height(tt13),1);
tt14 = unstack(tt13,'Dummy','ZIPCODE');
tt14 = fillmissing(tt14,'Constant',0);
caldiff(tt13.Date([1 end]),'days')
t1 = datetime(2012,1,1):caldays(1):datetime(2012,12,31);
tt15 = retime(tt14,t1,'FillWithConstant','Constant',0);
tt15.DoY = day(tt15.Date,'dayofyear');
tt16 = timetable2table(tt15,'ConvertRowTimes',false);
tt16 = varfun(@sum,tt16,'GroupingVariable','DoY');
tt15.MoY = month(tt15.Date);
tt15.DoM = day(tt15.Date);
tt15.WoY = week(tt15.Date);
tt16 = timetable2table(tt15,'ConvertRowTimes',false);
tt16 = varfun(@sum,tt16,'GroupingVariables',{'WoY' 'MoY' 'DoM'});
leapDayRows = (tt16.MoY==2) & (tt16.DoM==29);
tt16(leapDayRows,:) = [];
tt17 = varfun(@sum,tt16,'GroupingVariable','WoY')
%2013
tt18 = readtable('BackupByZipCode1.xlsx');
tt19=table2timetable(tt18)
tt19.ZIPCODE = categorical(tt19.ZIPCODE);
tt19.Dummy = ones(height(tt19),1);
tt20 = unstack(tt19,'Dummy','ZIPCODE');
tt20 = fillmissing(tt20,'Constant',0);
caldiff(tt19.Date([1 end]),'days')
t2 = datetime(2013,1,1):caldays(1):datetime(2013,12,31);
tt21 = retime(tt20,t2,'FillWithConstant','Constant',0);
tt21.DoY = day(tt21.Date,'dayofyear');
tt22 = timetable2table(tt21,'ConvertRowTimes',false);
tt22 = varfun(@sum,tt22,'GroupingVariable','DoY');
tt21.MoY = month(tt21.Date);
tt21.DoM = day(tt21.Date);
tt21.WoY = week(tt21.Date);
tt22 = timetable2table(tt21,'ConvertRowTimes',false);
tt22 = varfun(@sum,tt22,'GroupingVariables',{'WoY' 'MoY' 'DoM'});
leapDayRows = (tt22.MoY==2) & (tt22.DoM==29);
tt22(leapDayRows,:) = [];
tt23 = varfun(@sum,tt22,'GroupingVariable','WoY')
%2014
tt24 = readtable('BackupByZipCode1.xlsx');
tt25=table2timetable(tt24)
tt25.ZIPCODE = categorical(tt25.ZIPCODE);
tt25.Dummy = ones(height(tt25),1);
tt26 = unstack(tt25,'Dummy','ZIPCODE');
tt26 = fillmissing(tt26,'Constant',0);
caldiff(tt25.Date([1 end]),'days')
t3 = datetime(2014,1,1):caldays(1):datetime(2014,12,31);
tt27 = retime(tt26,t3,'FillWithConstant','Constant',0);
tt27.DoY = day(tt27.Date,'dayofyear');
tt28 = timetable2table(tt27,'ConvertRowTimes',false);
tt28 = varfun(@sum,tt28,'GroupingVariable','DoY');
tt27.MoY = month(tt27.Date);
tt27.DoM = day(tt27.Date);
tt27.WoY = week(tt27.Date);
tt28 = timetable2table(tt27,'ConvertRowTimes',false);
tt28 = varfun(@sum,tt28,'GroupingVariables',{'WoY' 'MoY' 'DoM'});
leapDayRows = (tt28.MoY==2) & (tt28.DoM==29);
tt28(leapDayRows,:) = [];
tt29 = varfun(@sum,tt28,'GroupingVariable','WoY')
%2015
tt30 = readtable('BackupByZipCode1.xlsx');
tt31=table2timetable(tt30)
tt31.ZIPCODE = categorical(tt31.ZIPCODE);
tt31.Dummy = ones(height(tt31),1);
tt32 = unstack(tt31,'Dummy','ZIPCODE');
tt32 = fillmissing(tt32,'Constant',0);
caldiff(tt31.Date([1 end]),'days')
t4 = datetime(2015,1,1):caldays(1):datetime(2015,12,31);
tt33 = retime(tt32,t4,'FillWithConstant','Constant',0);
tt33.DoY = day(tt33.Date,'dayofyear');
tt34 = timetable2table(tt33,'ConvertRowTimes',false);
tt34 = varfun(@sum,tt34,'GroupingVariable','DoY');
tt33.MoY = month(tt33.Date);
tt33.DoM = day(tt33.Date);
tt33.WoY = week(tt33.Date);
tt34 = timetable2table(tt33,'ConvertRowTimes',false);
tt34 = varfun(@sum,tt34,'GroupingVariables',{'WoY' 'MoY' 'DoM'});
leapDayRows = (tt34.MoY==2) & (tt34.DoM==29);
tt34(leapDayRows,:) = [];
tt35 = varfun(@sum,tt34,'GroupingVariable','WoY')
%2016
tt36 = readtable('BackupByZipCode1.xlsx');
tt37=table2timetable(tt36)
tt37.ZIPCODE = categorical(tt37.ZIPCODE);
tt37.Dummy = ones(height(tt37),1);
tt38 = unstack(tt37,'Dummy','ZIPCODE');
tt38 = fillmissing(tt38,'Constant',0);
caldiff(tt37.Date([1 end]),'days')
t5 = datetime(2016,1,1):caldays(1):datetime(2016,12,31);
tt39 = retime(tt38,t5,'FillWithConstant','Constant',0);
tt39.DoY = day(tt39.Date,'dayofyear');
tt40 = timetable2table(tt39,'ConvertRowTimes',false);
tt40 = varfun(@sum,tt40,'GroupingVariable','DoY');
tt39.MoY = month(tt39.Date);
tt39.DoM = day(tt39.Date);
tt39.WoY = week(tt39.Date);
tt40 = timetable2table(tt39,'ConvertRowTimes',false);
tt40 = varfun(@sum,tt40,'GroupingVariables',{'WoY' 'MoY' 'DoM'});
leapDayRows = (tt40.MoY==2) & (tt40.DoM==29);
tt40(leapDayRows,:) = [];
tt41 = varfun(@sum,tt40,'GroupingVariable','WoY')
%2017
tt42 = readtable('BackupByZipCode1.xlsx');
tt43=table2timetable(tt42)
tt43.ZIPCODE = categorical(tt43.ZIPCODE);
tt43.Dummy = ones(height(tt43),1);
tt44 = unstack(tt43,'Dummy','ZIPCODE');
tt44 = fillmissing(tt44,'Constant',0);
caldiff(tt43.Date([1 end]),'days')
t6 = datetime(2017,1,1):caldays(1):datetime(2017,12,31);
tt45 = retime(tt44,t6,'FillWithConstant','Constant',0);
tt45.DoY = day(tt45.Date,'dayofyear');
tt46 = timetable2table(tt45,'ConvertRowTimes',false);
tt46 = varfun(@sum,tt46,'GroupingVariable','DoY');
tt45.MoY = month(tt45.Date);
tt45.DoM = day(tt45.Date);
tt45.WoY = week(tt45.Date);
tt46 = timetable2table(tt45,'ConvertRowTimes',false);
tt46 = varfun(@sum,tt46,'GroupingVariables',{'WoY' 'MoY' 'DoM'});
leapDayRows = (tt46.MoY==2) & (tt46.DoM==29);
tt46(leapDayRows,:) = [];
tt47 = varfun(@sum,tt46,'GroupingVariable','WoY')
%2018
tt48 = readtable('BackupByZipCode1.xlsx');
tt49=table2timetable(tt48)
tt49.ZIPCODE = categorical(tt49.ZIPCODE);
tt49.Dummy = ones(height(tt49),1);
tt50 = unstack(tt49,'Dummy','ZIPCODE');
tt50 = fillmissing(tt50,'Constant',0);
caldiff(tt49.Date([1 end]),'days')
t7 = datetime(2018,1,1):caldays(1):datetime(2018,12,31);
tt51 = retime(tt50,t7,'FillWithConstant','Constant',0);
tt51.DoY = day(tt51.Date,'dayofyear');
tt52 = timetable2table(tt51,'ConvertRowTimes',false);
tt52 = varfun(@sum,tt52,'GroupingVariable','DoY');
tt51.MoY = month(tt51.Date);
tt51.DoM = day(tt51.Date);
tt51.WoY = week(tt51.Date);
tt52 = timetable2table(tt51,'ConvertRowTimes',false);
tt52 = varfun(@sum,tt52,'GroupingVariables',{'WoY' 'MoY' 'DoM'});
leapDayRows = (tt52.MoY==2) & (tt52.DoM==29);
tt52(leapDayRows,:) = [];
tt53 = varfun(@sum,tt52,'GroupingVariable','WoY')
%2019
tt54 = readtable('BackupByZipCode1.xlsx');
tt55=table2timetable(tt54)
tt55.ZIPCODE = categorical(tt55.ZIPCODE);
tt55.Dummy = ones(height(tt55),1);
tt56 = unstack(tt55,'Dummy','ZIPCODE');
tt56 = fillmissing(tt56,'Constant',0);
caldiff(tt55.Date([1 end]),'days')
t8 = datetime(2019,1,1):caldays(1):datetime(2019,12,31);
tt57 = retime(tt56,t8,'FillWithConstant','Constant',0);
tt57.DoY = day(tt57.Date,'dayofyear');
tt58 = timetable2table(tt57,'ConvertRowTimes',false);
tt58 = varfun(@sum,tt58,'GroupingVariable','DoY');
tt57.MoY = month(tt57.Date);
tt57.DoM = day(tt57.Date);
tt57.WoY = week(tt57.Date);
tt58 = timetable2table(tt57,'ConvertRowTimes',false);
tt58 = varfun(@sum,tt58,'GroupingVariables',{'WoY' 'MoY' 'DoM'});
leapDa
yRows = (tt58.MoY==2) & (tt58.DoM==29);
tt58(leapDayRows,:) = [];
tt59 = varfun(@sum,tt58,'GroupingVariable','WoY')
Yuck... :)
%2010
tt0 = readtable('BackupByZipCode1.xlsx');
tt1=table2timetable(tt0)
...
t = datetime(2010,1,1):caldays(1):datetime(2010,12,31);
...
%2011
tt6 = readtable('BackupByZipCode1.xlsx');
tt7=table2timetable(tt6)
...
t0 = datetime(2011,1,1):caldays(1):datetime(2011,12,31);
...
All is identical excepting for constants that can be coded as variables in a loop...there's no need for anything but one piece of code.
Unless the above is run in pieces and either the file is changed externally or the location in which the code is executed is different, then they're all using the identical data besides.
If the sections are using different copies of the file, then either externally rename them by year to be able to store in one common subdirectory or define an input table to specify where each is located.
Alternatively, just mung on the above and build the output table as an explicit catenation of each of the previous onto a global name.
Yes. Right. How would I bulid the output table as a catenation of each?
t=[];
...
t=[t;tt5];
...
t=[t;tt11];
...
t=[t;tt17];
...
Or, you can just string 'em all out in the end.
Realistically, the whole thing ought to be trashed and rewritten...
Thanks! About the code... Yes, I know

Sign in to comment.

Answers (0)

Categories

Products

Asked:

on 23 Jun 2020

Commented:

on 25 Jun 2020

Community Treasure Hunt

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

Start Hunting!