Data tip in app designer not working

26 views (last 30 days)
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

Accepted Answer

Adam Danz
Adam Danz on 26 Jan 2022
Edited: Adam Danz on 27 Jan 2022
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".

More Answers (0)

Categories

Find more on Develop uifigure-Based Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!