MATLAB: How to extract the x and y coordinates when hovering over a “heatmap”

datatipsheatmapMATLABposition;

I am using a "heatmap" object and I would like to be able to select one cell and be able to get the position of it, through code, not only by reading it from the dataTips window. Is there a property or a callback which allows me to do that?

Best Answer

As of MATLAB R2019b, this is not possible. Other objects such as "imagesc" allow the exact functionality that you require. In fact, you can use their "ButtonDownFcn" method to extract the coordinates of the point where you click in the figure. Therefore, as a workaround, you could use the following script to create an image object which looks like a "heatmap" and extract the position of the mouse:
d = magic(5);
im = imagesc(d);
[x,y] = meshgrid(1:5);
labels = num2str(d(:));
text(x(:),y(:),labels);
im.ButtonDownFcn = @(s,e) disp(e)