MATLAB: How to modify the X and Y axis tick labels for a Heatmap chart

MATLABxticklabelsxticksyticklabelsyticks

How can I modify the X and Y axis tick labels for a 'Heatmap' chart?
My data is too dense (100 ticks on each axis) and tick labels overlap each other if all of them are plotted.
For example, how can I only display every '5th' tick label, instead of all of them?

Best Answer

I understand that you are trying to change the displayed tick labels on the X and Y axis of a 'Heatmap'.
For this you need to set the 'XDisplayLabels' property of the heatmap object.
The following link provides more information on this:
<https://www.mathworks.com/help/matlab/ref/matlab.graphics.chart.heatmapchart-properties.html#d117e582387>
I created an example MATLAB script to demonstrate how this may be done, as follows:
% Create a random 2-D matrix and make a heatmap
A = randi(100,100);
h = heatmap(A);
Create custom X tick labels such that every tick label is blank (i.e., " ") except ticks which are divisible by 5 (i.e., 5, 10, 15, ...)
XLabels = 1:100;
% Convert each number in the array into a string
CustomXLabels = string(XLabels);
% Replace all but the fifth elements by spaces
CustomXLabels(mod(XLabels,5) ~= 0) = " ";
% Set the 'XDisplayLabels' property of the heatmap
% object 'h' to the custom x-axis tick labels
h.XDisplayLabels = CustomXLabels;
In the above example we create a random (100 X 100) matrix of numbers 'A' and use it to create a Heatmap 'h':
Assuming that we only want to display every 5th tick label on the X-axis of the Heatmap, I create a (1 X 100) array of strings 'CustomXLabels'.
Every element of 'CustomXLabels' is an white space (i.e., ' ' ) except for the ones that are divisible by 5 (these elements will be strings like: '5', '10', '15', ...., '100').
Next, we set the 'XDisplayLabels' property of the Heatmap object 'h' with the custom labels array 'CustomXLabels', which sets the X-Axis tick labels as per our requirements: