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 best to add Plugin Capability to a Delphi program

delphi plugins

I am looking to add the capability for users to write plugins to the program I have developed in Delphi. The program is a single executable with no DLLs used.

decltype(auto) vs auto&& to perform generic handling of function's return type

c++ c++14

When using auto&& to handle a function returning a lvalue: