Strings as HeatMap labels

I have a 40x40 matrix as my dataset and I want to assign string variables as row and column labels to the heat map generated using MATLAB. I am unable to interpret the help page syntax correctly. Can someone help me?

Answers


The principle is always the same. First a more generic example, and below especially for the HeatMap function.


I assume your x and y data is not the actual index vector, therefore.

% example data
x = linspace(-20,40,40);
y = linspace(20,100,40);
[X,Y] = meshgrid(x,y);
heat = X.*Y;

% the plot
figure(1)
surf(X,Y,heat)
view(0,90)
colormap(hot)
colorbar

% the ticks
xticks = 1:numel(x);
yticks = 1:numel(y);

set(gca,'XTick',x,'XTickLabels',xticks)
set(gca,'YTick',y,'YTickLabels',xticks)

I personally would skip every second value and you can also move the labels, so they are located under every colored column/row, not inbetween/on the grid.

set(gca,'XTick',x(1:2:end)+(x(2)-x(1))/2,'XTickLabels',xticks(1:2:end))
set(gca,'YTick',y(1:2:end)+(x(2)-x(1))/2,'YTickLabels',xticks(1:2:end))


And for the HeatMap function. Same principle again just that you need a cell array of strings. For example I used the numbers 1 to 40 as strings. You can substitute it with your string cell array.

% same example data as above
xticks = 1:numel(x);
yticks = 1:numel(y);
%xticks as string cell array
xStrings = arrayfun(@num2str, xticks, 'Uniform', false);  
%yticks as string cell array
yStrings = arrayfun(@num2str, yticks, 'Uniform', false);  
HeatMap(heat,'RowLabels', yStrings, 'ColumnLabels', xStrings)


or a little more stringlike:

labelString = 'helloworldhelloworldhelloworldhelloworld';
labels = strsplit(sprintf('%c ',labelString),' ');
labels = labels(1:end-1);
HeatMap(heat,'RowLabels', fliplr(labels), 'ColumnLabels', labels)


Need Your Help

how to fire tab key event

java swing awt

How do we fire a "tab" key pressed event deliberately in java.? I also want to know how to fire a "Shift+tab" key pressed event programatically in java. Though this a bit lame question,please bear...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.