MATLAB: How to add seperation lines in imagesc while maintaining label positions centered

grid with centered labelsimagescMATLAB

Hello everyone,
I would like to add seperation lines in my correlation matrices but maintain my labels centered. I have tried "minorticks" option but that doesn't work. I use imagesc to plot correlation matrices in the following way:
C = rand(3,3); % dummy data
labels = {'A' 'B' 'C'}; % dummy labels
n = length(labels);
corrmat = corrcoef(C); % correlation matrix
figure()
im = imagesc(corrmat); % plot the matrix

im.AlphaData = .5; % for visibility in this example
set(gca, 'XTick', 1:n); % center x-axis ticks on bins
set(gca, 'YTick', 1:n); % center y-axis ticks on bins
set(gca, 'XTickLabel', labels,'FontSize',8); % set x-axis labels

set(gca, 'YTickLabel', labels,'FontSize',8); % set y-axis labels

Which gives me the following plot with the labels where I want them.
When I want to add the seperation lines (grid) I have the problem, that my labels obviously also move to the edges.
[nx, ny] = size(corrmat);
figure()
im = imagesc(corrmat); % plot the matrix
im.AlphaData = .5;
set(gca,'xtick', linspace(0.5,nx+0.5,nx+1), 'ytick', linspace(0.5,ny+.5,ny+1));
set(gca,'xgrid', 'on', 'ygrid', 'on', 'gridlinestyle', '-', 'gridcolor','k',...
'gridalpha',1,'xcolor', 'k', 'ycolor', 'k','LineWidth',1);
set(gca, 'XTickLabel', labels,'FontSize',8); % set x-axis labels
set(gca, 'YTickLabel', labels,'FontSize',8); % set y-axis labels
What I want though, is a grid with centered labels. I assume that I can't realise that with the axes alone. Any idea for a nice workaround? I'd appreciate it.
BR

Best Answer

You can use xline() and yline()
C = rand(3,3); % dummy data
labels = {'A' 'B' 'C'}; % dummy labels
n = length(labels);
corrmat = corrcoef(C); % correlation matrix
figure()
im = imagesc(corrmat); % plot the matrix
im.AlphaData = .5; % for visibility in this example
set(gca, 'XTick', 1:n); % center x-axis ticks on bins
set(gca, 'YTick', 1:n); % center y-axis ticks on bins
set(gca, 'XTickLabel', labels,'FontSize',8); % set x-axis labels
set(gca, 'YTickLabel', labels,'FontSize',8); % set y-axis labels
hold on
xs = 0.5:n+0.5;
ys = 0.5:n+0.5;
lx = arrayfun(@(x) xline(x, 'LineWidth', 1), xs) % same as for-loop, but compact
ly = arrayfun(@(y) yline(y, 'LineWidth', 1), ys)