Data tip in app designer not working

16 次查看(过去 30 天)
Hi,
Could some please explain to me why data tips can be displayed in the first code but not in the second code? For example whenever index is negative the second code does not work.
First code:
% Create image data
imageData = randi(100,10,12);
% Show the same image in uifigure and turn on datacursor
uifig = uifigure();
uiax = uiaxes(uifig, 'Position', [10 10 500 410]);
imagesc(uiax,imageData);
% imagesc(uiax,X,Y,Z);
uiax.Colormap = jet(100);
axis(uiax,'tight')
title(uiax,'uifigure')
% Step 1: Turn on datacursor mode in the UIFigure
% uifig is the figure handle to the uifigure or the app
dcm = datacursormode(uifig);
dcm.Enable = 'on';
% Step 2: Define a custom UpdateFcn function
% uiax is the handle to the UIAxes
dcm.UpdateFcn = @(hObj,event,ax)localDcmFcn(hObj,event,uiax);
Second code:
uifig = uifigure();
uiax = uiaxes(uifig);
Z = peaks;
X = linspace(0,2*pi);
Y = linspace(0,2*pi);
imagesc(uiax,X,Y,Z)
% Step 1: Turn on datacursor mode in the UIFigure
% uifig is the figure handle to the uifigure or the app
dcm = datacursormode(uifig);
dcm.Enable = 'on';
% Step 2: Define a custom UpdateFcn function
% uiax is the handle to the UIAxes
dcm.UpdateFcn = @(hObj,event,ax)localDcmFcn(hObj,event,uiax);
Custom Function:
function txt = localDcmFcn(~,event,ax)
% Compute index
idx = event.Target.CData(event.Position(2),event.Position(1));
% Create output text
txt = sprintf('[X,Y] [%f %f]\nIndex %f\n[R,G,B] [%.4f %.4f %.4f]', ...
event.Position, idx, ax.Colormap(idx,:));
end

采纳的回答

Adam Danz
Adam Danz 2022-1-26
编辑:Adam Danz 2022-1-27
The code in OP's question is copied/modified from this answer, for reference.
The difference between the first and second blocks of code in your question is that in block 2 you are specifying x and y values in the image whereas in block 1, imagesc is creating the x and y values. When imagesc creates the x and y values, they are integer indices which makes indexing within the localDcmFcn straightforward. When you specify the x and y values, the XData and YData in the image are no longer integers that correspond with the indices of CData so they can't be used as indices.
To illustrate this point, consider this line and look at the event.Position values produced by both block 1 and block 2 in your question. In block 1, those values will be integers that can be indexed into CData. In block 2, those values are floating point decimals that cannot be used as indices.
idx = event.Target.CData(event.Position(2),event.Position(1));
To make block 2 work, see the updated answer in this link section "Solution using scaled images".

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by