-
Notifications
You must be signed in to change notification settings - Fork 16
/
imagesctxt.m
executable file
·33 lines (26 loc) · 1.19 KB
/
imagesctxt.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function imagesctxt(mat,clim)
if nargin==1
clim=[];
end
%IMAGESCTXT Scale data and display as image with text of values impressed.
% IMAGESCTXT(...) is the same as IMAGESC(...) except the values are
% printed on pixels.
%
% Carlo Nicolini, Istituto Italiano di Tecnologia (2015).
%
imagesc(mat,clim);
colormap(parula);
textStrings = num2str(mat(:),'%1.3g'); % Create strings from the matrix values
textStrings = strtrim(cellstr(textStrings)); % Remove any space padding
[x,y] = meshgrid(1:size(mat,2),1:size(mat,1)); % Create x and y coordinates for the strings
% Plot the strings
hStrings = text(x(:),y(:),textStrings(:), 'HorizontalAlignment','center');
midValue = mean(get(gca,'CLim')); % Get the middle value of the color range
% text color of the strings so they can be easily seen over the background color
textColors = repmat(mat(:) < midValue,1,3);
set(hStrings,{'Color'},num2cell(textColors,2)); % Change the text colors
set(gca,'XTick',1:size(mat,2),... % Change the axes tick marks
'XTickLabel',num2cell(1:size(mat,2)),... % and tick labels
'YTick',1:size(mat,1),...
'YTickLabel',num2cell(1:size(mat,1)),...
'TickLength',[0 0]);