MATLAB: How to add vertical and horizontal lines to the confusion matrix generated with matlab

axisimageimagesc

confmat = C;
labels = {'0', '1', '2', '3', '11' };
numlabels = size(confmat, 1); % number of labels
confpercent = 100*confmat./repmat(sum(confmat, 1),numlabels,1);
imagesc(confpercent);
Mycolors=[0 0.7 0.4; 1 0.9 0.9 ]
colormap(flipud(Mycolors));
textStrings = num2str([confpercent(:)], '%.1f%%\n');
textStrings = strtrim(cellstr(textStrings));
[x,y] = meshgrid(1:numlabels);
hStrings = text(x(:),y(:),textStrings(:), ...
'HorizontalAlignment','center');
midValue = mean(get(gca,'CLim'));
textColors = repmat(confpercent(:) > midValue,1,3);
set(hStrings,{'Color'},num2cell(textColors,2));
set(gca,'XTick',1:numlabels,... 'XTickLabel',labels,... 'YTick',1:numlabels,... 'YTickLabel',labels,... 'TickLength',[0 0]);
From the above code I got the next picture
How it is possible to add horizontal lines to get a CM like the next :

Best Answer

Do you have access the the Statistics and Machine Learning Toolbox? If so, conisder using the confusionmat and confusionchart functions.
If not, you can use the new xline and yline functions (introduced in R2018b):
xline(1.5);
xline(2.5);
xline(3.5);
xline(4.5);
yline(1.5);
yline(2.5);
yline(3.5);
yline(4.5);
If you don't have 18b, the equivalent code for a single line is:
hold on
plot([1.5 1.5],get(gca,'YLim'),'Color',[0.15,0.15,0.15],'LineWidth',0.5)
...
plot(get(gca,'XLim'),[1.5 1.5],'Color',[0.15,0.15,0.15],'LineWidth',0.5)
...