MATLAB: Create a map with 3 different variables

MATLABpivottable

I am trying to create a table that has bins of RPM on the x-axis, bins of throttle position on the Y-axis, and filled in the middle based on these valuse is the fuel used (z-axis). The fuel used vairable needs to be the average of all fuel values that fall in the RPM range and throttle range it falls in and place that average value in the corresping cell.
The code I have so far to try and do this is attached as well as the data file I am using. Also a screenshot of the table I am trying to recreate is attached.
Thanks in advance.

Best Answer

Plotting bivariate binned means using heatmap
% Compute binned mean
[N,~,~,binX,binY] = histcounts2(RPM, Throttle, binEdgesRPM, binEdgesThrottle)
ind = sub2ind(size(N),binX,binY);
binMeans = reshape(arrayfun(@(i)mean(Fuel(ind==i)), 1:numel(N)),size(N));
% Compute bin centers (for heatmap)
binCentersRPM = binEdgesRPM(2:end)-(binEdgesRPM(2)-binEdgesRPM(1))/2;
binCentersThrottle = binEdgesThrottle(2:end)-(binEdgesThrottle(2)-binEdgesThrottle(1))/2;
% Plot heatmap
heatmap(binCentersRPM, binCentersThrottle, binMeans.')
... using imagesc
imagesc(binEdgesRPM, binEdgesThrottle, binMeans')
Plotting bivariates binned counts
histogram2() or histcounts2() + heatmap()
filename = 'Scarface_tune_run2.csv';
Raw = xlsread(filename);
%% Throttle Base Injection Qty Map
RPM = Raw(:,4);
Throttle= Raw(:,5);
Fuel = Raw(:,11);
Map1 = cat(2,RPM,Throttle,Fuel);
topEdge = 4800;
botEdge = 0;
numBins = 24;
binEdgesRPM = linspace(botEdge, topEdge, numBins+1);
binEdgesThrottle = linspace(0, 100, 11);
% THIS LINE BELOW PRODUCES THE PLOT
histogram2(RPM, Throttle, binEdgesRPM, binEdgesThrottle, 'DisplayStyle','tile','ShowEmptyBins','on')
Or
N = histcounts2(RPM, Throttle, binEdgesRPM, binEdgesThrottle)
heatmap(binEdgesRPM, binEdgesThrottle,N)