MATLAB: Add titles over rows in subplots

figureMATLABsubplottitle

Hi,
I have a figure with 8 subplots, arranged in two rows. I need to generate an automated code adding a centered title above each row, but fail to do so.
A sample code to illustrate the configuration (I also have annotations):
clf;
m = 2;
n = 4;
p = 1;
for i = 1:m*n
s(i) = subplot(m,n,p);
ant(i) = annotation('textbox',s(i).Position,'String',[num2str(i)+")"],...
'linestyle', 'none','Fontsize',14,'FontWeight','bold');
p = p + 1;
end
I want to place the titles without explictly give coordinates, i.e. something like titlehandle.position=[1 2 3 4] is not good, because I sometime use different computers/monitors/OS and the output is not consistent.
Thanks!

Best Answer

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:
figure()
subplotLayout = [3,3];
ax = gobjects(fliplr(subplotLayout));
for i = 1:prod(subplotLayout)
ax(i) = subplot(subplotLayout(1),subplotLayout(2),i);
end
ax = ax'; % now axis handles are same shape as subplot layout

% list all titles in order (top to bottom)

titles = {'Row 1 title', 'Row 2 title', 'Row 3 title'};
% Get handles to center subplots
centerSubHandles = ax(:,ceil(subplotLayout(2)/2));
for i = 1:numel(centerSubHandles)
% get center subplot handle
title(centerSubHandles(i), titles{i})
end
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:
figure()
subplotLayout = [3,4];
ax = gobjects(fliplr(subplotLayout));
for i = 1:prod(subplotLayout)
ax(i) = subplot(subplotLayout(1),subplotLayout(2),i);
end
set(ax,'Units','Normalize') % this is typically the default
ax = ax'; % now axis handles are same shape as subplot layout
% Reduce vertical space of axes just a bit to make room for "titles"
axPos = cell2mat(get(ax, 'Position'));
axPos(:,4) = axPos(:,4).*.96; % reduces vert space to 96% of height
set(ax, {'Position'}, mat2cell(axPos, ones(numel(ax),1), 4))
% Get upper position of each row of axes, normalized coordinates
axPos = cell2mat(get(ax(:,1), 'Position'));
axUpperPos = sum(axPos(:,[2,4]),2); %upper pos.
% Get center position for 1st row (assumes all rows have same center)
axPos = cell2mat(get(ax(1,[1,end]),'Position'));
axCenterPos = mean([axPos(1,1), sum(axPos(2,[1,3]))]);
% list all titles in order (top to bottom)
titles = {'Row 1 title', 'Row 2 title', 'Row 3 title'};
% Set annotation for each row of subplots
titleHandles = gobjects(numel(titles),1);
for i = 1:numel(titles)
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, ... % matches title property
'FontSize', ax(1).Title.FontSize, ... % matches title property
'FontName', ax(1).Title.FontName, ... % matches title property
'Color', ax(1).Title.Color); % matches title property
end