Data tip in app designer not working
조회 수: 11 (최근 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
댓글 수: 0
채택된 답변
Adam Danz
2022년 1월 26일
편집: Adam Danz
2022년 1월 27일
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));
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Develop uifigure-Based Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!