It drives me nutz that heatmap properties are special cases. To set the aspect ratio of the heatmap, you need to adjust the axis or figure size based on the number or rows & columns. Below are two functional demos. The first adjusts the axis size and recenters it. The second adjusts the figure size instead. I recommend the first method so the heatmap title and other graphics won't potentially expand beyond the figure boundaries.
Method 1: Adjust axis size and re-center
load patients
tbl = table(LastName,Age,Gender,SelfAssessedHealthStatus,...
Smoker,Weight,Location);
h = heatmap(tbl,'Smoker','SelfAssessedHealthStatus');
originalUnits = h.Units;
h.Units = 'centimeters';
sz = size(h.ColorData);
originalPos = h.Position;
h.Position(3:4) = min(h.Position(3:4))*[1,1];
if sz(1)>sz(2)
h.Position(3) = h.Position(3)*(sz(2)/sz(1));
h.Position(1) = (originalPos(1)+originalPos(3)/2)-(h.Position(3)/2);
else
h.Position(4) = h.Position(4)*(sz(1)/sz(2));
h.Position(2) = (originalPos(2)+originalPos(4)/2)-(h.Position(4)/2);
end
h.Units = originalUnits;
Method 2: Adjust figure size
load patients
tbl = table(LastName,Age,Gender,SelfAssessedHealthStatus,...
Smoker,Weight,Location);
h = heatmap(tbl,'SelfAssessedHealthStatus','Smoker');
fh = gcf();
originalUnits.fig = fh.Units;
originalUnits.ax = h.Units;
fh.Units = 'centimeters';
h.Units = 'Normalize';
sz = size(h.ColorData);
h.Position(3:4) = min(h.Position(3:4))*[1,1];
fh.InnerPosition(3:4) = min(fh.InnerPosition(3:4))*[1,1];
if sz(1)>sz(2)
fh.InnerPosition(3) = fh.InnerPosition(3)*(sz(2)/sz(1));
else
fh.InnerPosition(4) = fh.InnerPosition(4)*(sz(1)/sz(2));
end
fh.Units = originalUnits.fig;
h.Units = originalUnits.ax;
Best Answer