%% Separate One Variable into Three Variables
% Create a table indicating the amount of snowfall in various towns for various storms. Specify the towns using a categorical array, since there are a fixed set of town names in this table.

Storm = [3;3;1;3;1;1;4;2;4;2;4;2];
Town = categorical({'Natick';'Worcester';'Natick';'Boston';'Boston';'Worcester';...
                    'Boston';'Natick';'Worcester';'Worcester';'Natick';'Boston'});
Snowfall = [0;3;5;5;9;10;12;13;15;16;17;21];

S = table(Storm,Town,Snowfall)

% S contains three snowfall entries for each storm, one for each town. S is in stacked format, with Town having the categorical data type. Table variables that have the categorical data type are useful indicator variables and grouping variables for unstacking.
%
% Separate the variable Snowfall into three variables, one for each town specified in the variable, Town. The output table, U, is in unstacked format.

U = unstack(S,'Snowfall','Town')

% Each row in U contains data from rows in S that have the same value in the grouping variable, Storm. The order of the unique values in Storm determines the order of the data in U.
% Copyright 2012 The MathWorks, Inc.