MATLAB: How to display the numerical values of each cell as text in the PCOLOR plot

MATLABnumberspcolorshowtext;values

I am creating plots using PCOLOR. In addition to the colored cells representing the values I would also like to display the numerical value in each element of the PCOLOR plot.

Best Answer

The ability to systematically plot matrices as text cells is not available in MATLAB. It is possible to include labels and comments in plots using TEXT or ANNOTATION commands. However the creation of the plot will be very slow if you are using this on large data sets.
See the following example to get an idea how this could be implemented:
%create the PCOLOR plot
matrix=(rand(5));
pcolor(1:5,1:5,matrix);
colorbar;
%prepare position and size of textboxes
pos=get(gca,'position');
[rows,cols]=size(matrix);
width=pos(3)/(cols-1);
height =pos(4)/(rows-1);
%create textbox annotations
for i=1:cols-1
for j=rows-1:-1:1 annotation('textbox',[pos(1)+width*(i-1),pos(2)+height*(j-1),width,height], ...
'string',num2str(matrix(j,i)),'LineStyle','none','HorizontalAlignment','center',...
'VerticalAlignment','middle');
end
end
The estimation of the text values assumes you are using flat shading. This means each element has a constant color determined by the color value at the endpoint of the segment or the corner of the face that has the smallest index or indices. If you are not using flat shading you have to adjust the example accordingly.