Display values of each element in imagesc

120 views (last 30 days)
I have a 16 x 16 array of values between 0 and 40. I would like to display this using imagesc, but I would also like to superimpose the decimal value of each element in its corresponding square. I would end up with a 16 x 16 array of colored (per mapping) squares, each with the square's value in decimal text. Is there an easy / convenient way to do this?
If there was a version of "disp" that I could apply to a figure after imagesc that might work, but I can't find one.
Thanks

Accepted Answer

Chunru
Chunru on 11 Oct 2021
c = randi([0 40], [16 16]);
heatmap(c);
colormap(jet(512))

More Answers (1)

Walter Roberson
Walter Roberson on 11 Oct 2021
Is there an easy / convenient way to do this?
No. But it can be done.
Note: there is no one text font color that will contrast nicely with all of the underlaying colors -- not unless you colormap() in a different colormap such as copper() that you can find a simple text color for.
A = randi([0 40], 16, 16); %sample data
[R, C] = ndgrid(1:size(A,1), 1:size(A,2));
R = R(:); C = C(:) - 1/4;
%rows are Y values, columns are X values !
imagesc(A)
vals = A(:);
mask = vals <= 20;
text(C(mask), R(mask), string(vals(mask)), 'color', 'w')
text(C(~mask), R(~mask), string(vals(~mask)), 'color', 'k')
A
A = 16×16
28 33 29 4 1 22 13 4 7 6 16 27 34 39 7 11 40 14 37 8 14 25 10 12 6 40 0 40 27 28 29 21 6 3 16 31 35 3 31 23 7 18 40 19 39 2 7 6 15 36 7 6 1 5 18 33 27 25 22 14 14 8 16 38 27 7 39 37 11 15 28 1 4 32 8 37 7 38 29 32 0 3 32 34 10 9 1 26 17 17 19 5 15 16 6 9 37 4 13 10 34 23 29 37 36 16 19 26 4 5 19 37 14 23 32 15 5 11 26 37 4 3 14 22 22 27 5 21 6 33 16 13 31 23 25 37 33 1 14 5 0 21 6 18 22 2 21 15 11 25 1 18 14 23 9 19 8 19 17 6
  1 Comment
Peter Fraser
Peter Fraser on 11 Oct 2021
Thanks. I was trying something similar, but it looks like the heatmap might be ideal for my needs.

Sign in to comment.

Categories

Find more on Colormaps in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!