MATLAB: How to label each pixel of the output of IMAGESC with its value

MATLAB

I have drawn an image with the IMAGESC command and would like to label each pixel with its value.

Best Answer

The following code illustrates how this might be done:
% Generate Random Data
N = 5;
M = rand(N);
x = repmat(1:N,N,1); % generate x-coordinates
y = x'; % generate y-coordinates
% Generate Labels
t = num2cell(M); % extact values into cells
t = cellfun(@num2str, t, 'UniformOutput', false); % convert to string
% Draw Image and Label Pixels
imagesc(M)
text(x(:), y(:), t, 'HorizontalAlignment', 'Center')
Related Question