%% Stack Variables and Specify Variable Names
% Create a timetable indicating the amount of snowfall in three towns from five different storms. Specify the dates of the storms as datetime values and use them as the row times of the timetable U. Specify the array of storm numbers, Storm, as a categorical array since there is a fixed set of storm numbers in this timetable.

Storm = categorical([1;2;3;4;5]);
Date = datetime({'2011-12-25';'2012-01-02';'2012-01-23';'2012-02-07';'2012-02-15'});
Natick = [20;5;13;0;17];
Boston = [18;9;21;5;12];
Worcester = [26;10;16;3;15];

U = timetable(Date,Storm,Natick,Boston,Worcester)

% The variables Storm and Date contain data that is constant at each location.
%
% Stack the variables Natick, Boston, and Worcester into a single variable. Name the variable containing the stacked data, Snowfall, and name the new indicator variable, Town.

S = stack(U,{'Natick','Boston','Worcester'},...
          'NewDataVariableName','Snowfall',...
          'IndexVariableName','Town')

% S contains three rows for each storm, and stack repeats the data in the constant variables, Storm and Date, accordingly.
% The categorical variable, Town, identifies which variable in U contains the corresponding Snowfall data.
% Copyright 2012 The MathWorks, Inc.