Interactive data tip specify x and y location

1 次查看(过去 30 天)
Hello,
I would like to plot a heatmap or an image and use data tip where I can manually enter the x and y location in a sperate window and get the data tip displayed and/or the z-coordinate displayed in the image or in a new window.
So I would interactively enter different x and y location and always get the correspoding z value.
How can I do this?
Thanks.

回答(1 个)

Narvik
Narvik 2024-9-2
Hi Konvictus177,
As per my understanding, you would like to create an interactive plot where you can enter x and y coordinates to display the corresponding z-value from a matrix.
You can do this via MATLAB GUI. Refer to the following documentation link for more information on MATLAB GUI:
The following example code demonstrates how to achieve interactive plot with input coordinates using a simple GUI with a heatmap and a text input for coordinates.
function interactiveHeatmap()
% create example data 10x10 data
data = peaks(10);
[xSize, ySize] = size(data);
% create figure and heatmap
hFig = figure('Name', 'Interactive Heatmap', 'NumberTitle', 'off');
hMap = heatmap(data);
title('Interactive Heatmap');
xlabel('X');
ylabel('Y');
% create uicontrols for input
uicontrol('Style', 'text', 'Position', [20 20 100 20], 'String', 'Enter X:');
xInput = uicontrol('Style', 'edit', 'Position', [130 20 50 20]);
uicontrol('Style', 'text', 'Position', [200 20 100 20], 'String', 'Enter Y:');
yInput = uicontrol('Style', 'edit', 'Position', [310 20 50 20]);
% button to update data tip
uicontrol('Style', 'pushbutton', 'Position', [380 20 100 20], ...
'String', 'Show Z', 'Callback', @updateDataTip);
% create a textbox for displaying the z-value
zText = annotation('textbox', [0.15, 0.85, 0.1, 0.1], 'String', '', ...
'FitBoxToText', 'on', 'BackgroundColor', 'white');
% function to update the z value
function updateDataTip(~, ~)
% get input values
x = str2double(get(xInput, 'String'));
y = str2double(get(yInput, 'String'));
% validate input
if isnan(x) || isnan(y) || x < 1 || y < 1 || x > xSize || y > ySize
errordlg('Invalid coordinates', 'Error');
return;
end
% get the z-value
z = data(y, x);
% update the text annotation to display the z-value
set(zText, 'String', sprintf('Z: %.3f', z));
end
end
I hope this helps!

类别

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