Create one centered title per row of subplots
Two methods are included
- For an odd number of subplots per row (simpler)
- For and even or odd number of subplots per row
For an odd number of suplots per row
Add the title to the middle subplot
Demo:
ax = gobjects(fliplr(subplotLayout));
for i = 1:prod(subplotLayout)
ax(i) = subplot(subplotLayout(1),subplotLayout(2),i);
titles = {'Row 1 title', 'Row 2 title', 'Row 3 title'};
centerSubHandles = ax(:,ceil(subplotLayout(2)/2));
for i = 1:numel(centerSubHandles)
title(centerSubHandles(i), titles{i})
For an even or odd number of suplots per row
Compute the upper position of each row of subplots in normalized units and the center position across the first row of subplots (assuming the center position is the same for all rows of subplots). Then use annotation to set the title position.
Demo:
ax = gobjects(fliplr(subplotLayout));
for i = 1:prod(subplotLayout)
ax(i) = subplot(subplotLayout(1),subplotLayout(2),i);
set(ax,'Units','Normalize')
axPos = cell2mat(get(ax, 'Position'));
axPos(:,4) = axPos(:,4).*.96;
set(ax, {'Position'}, mat2cell(axPos, ones(numel(ax),1), 4))
axPos = cell2mat(get(ax(:,1), 'Position'));
axUpperPos = sum(axPos(:,[2,4]),2);
axPos = cell2mat(get(ax(1,[1,end]),'Position'));
axCenterPos = mean([axPos(1,1), sum(axPos(2,[1,3]))]);
titles = {'Row 1 title', 'Row 2 title', 'Row 3 title'};
titleHandles = gobjects(numel(titles),1);
titleHandles = annotation('textbox','String',titles{i}, ...
'Position', [axCenterPos, axUpperPos(i), 0, 0], ...
'HorizontalAlignment', 'center','VerticalAlignment','bottom',...
'LineStyle','none','FitBoxToText','on', ...
'FontWeight',ax(1).Title.FontWeight, ...
'FontSize', ax(1).Title.FontSize, ...
'FontName', ax(1).Title.FontName, ...
'Color', ax(1).Title.Color);
Best Answer