Interactive data tip specify x and y location

조회 수: 1 (최근 30일)
Konvictus177
Konvictus177 2022년 3월 21일
답변: Narvik 2024년 9월 2일
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
Mathieu NOE
Mathieu NOE 2022년 3월 22일
hello
would suggest you look at this excellent FEX submission
plenty of demo examples

댓글을 달려면 로그인하십시오.

답변 (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