How do I interact with heatmaps?

3 次查看(过去 30 天)
Hello
In matlab appDesigner I have a heatmap assigned to a UIFigure:
h = heatmap(app.UIFigure,Matrix)
I would like to create a function that gives as a result the coordinates of a selected pixel. For example, if I have this heatmap
Matrix = [1 2 3
4 5 6
7 8 9]
and I click in the heatmap the pixel corresponding to the number 6, I would like to receive a [2,3]. Is it possible?

采纳的回答

Cris LaPierre
Cris LaPierre 2021-12-21
It would appear that functionality is not currently possible with heatmap (see this answer).
A possible workaround is to create the same visualization using imagesc (code shared in linked post). You can use the ButtonDown callback function to get the (x,y) coordinates of the cursor when you left click on the axes (see this answer for more details on how to set this up).
The final process is to do some maniputation of the cursor (x,y) to get the corresponding (row, column) of the heatmap.
Here's the code I came up with. Note that I put the code to create the heatmap in my app's StartupFcn callback.
% Code that executes after component creation
function startupFcn(app)
% create heatmap
Matrix = [1 2 3
4 5 6
7 8 9];
imagesc(app.UIAxes,Matrix,"HitTest","off")
% set colormap to match that of a heatmap
C = [0.9000 0.9447 0.9741
0 0.4470 0.7410];
cmap = interp1([1 256],C,1:256);
colormap(app.UIAxes,cmap)
colorbar(app.UIAxes)
% Add text labels to each box
[x,y] = meshgrid(1:length(Matrix));
labels = num2str(Matrix(:));
text(app.UIAxes,x(:),y(:),labels);
% Set X and Y axes tick locations
xticks(app.UIAxes,1:length(Matrix))
yticks(app.UIAxes,1:length(Matrix))
end
% Button down function: UIAxes
function UIAxesButtonDown(app, event)
% extract (y x) to get (row,column).
% Addition combined with floor returns pixel (R,C)
bx = floor(event.IntersectionPoint([2 1]) + .5)
end
When I run the app and click on 6, the value of bx displays in the command window.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by