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!