%% Compute Color Data Using Custom Aggregation Method
% Create a heatmap and color the cells using data computed with a custom aggregation method. Use the accumarray function to compute the color data.
% Read the sample file Temperature.csv into a table. The file contains three columns: Month, Year, and TemperatureF.

tbl = readtable('TemperatureData.csv');

%
% Create categorical arrays from the Month and Year columns of the table. Then determine the unique months and years to use as labels along the x-axis and y-axis.

months = categorical(tbl.Month);
years = categorical(tbl.Year);
xlabels = categories(months);
ylabels = categories(years);

%
% Determine the final size of the resulting color data based on the number of unique months and years.

nummonths = numel(xlabels);
numyears = numel(ylabels);

%
% Convert the categorical months and years arrays into numeric indices to use with the accumarray function. Compute the color data as the maximum temperature for each month and year combination using the accumarray function. Use NaN for missing month and year combinations.

x = double(months);
y = double(years);
temps = tbl.TemperatureF;
cdata = accumarray([y,x],temps,[numyears,nummonths],@max,NaN);

%
% Create the heatmap. Label the x-axis and y-axis with the months and years, respectively. Color the heatmap cells using the computed matrix data.

h = heatmap(xlabels,ylabels,cdata);

% Note: You can use the reordercats function for categorical arrays to reorder the axis labels.
% Copyright 2012 The MathWorks, Inc.