我正在尝试在 Matlab 中设计和编程 GUI,但我对此并不熟悉。
基本上,我有两个组件,即“轴”和“列表框”。轴中有一个 RGB 图像。我计划将选定的点添加到列表框中。
下面的代码工作得很好,但我想让它在数据游标打开时工作。
当数据光标打开时如何使其工作?
% 100x100x3 RGB image
RgbImage = randi(100, 100, 100, 3);
% Draw the image
axesHandle = axes();
imageHande = imagesc(RgbImage);
axis image;
% ButtonDownFc
set(imageHandle, 'ButtonDownFcn', @imageButtonDownFcn);
function imageButtonDownFcn(hObject, eventdata)
p = get(gca, 'CurrentPoint');
x = floor( p(1) );
y = floor( p(2) );
% Some code to add the [x y] to the list box
end
编辑1:
问题是这个函数图像 ButtonDownFcn当数据游标打开时不会触发。
我首先为数据游标创建一个自己的更新函数
% in your main .m file
hdt = datacursormode;
set(hdt,'UpdateFcn',{@labeldtips,hdt});
然后你可以像这样获取该函数中的位置:
function output_txt = labeldtips(obj,event_obj,hdt)
% Display an observation's Y-data and label for a data tip
% obj Currently not used (empty)
% event_obj Handle to event object
dcs=hdt.DataCursors;
pos = get(dcs(1),'Position'); %Position of 1st cursor
output_txt{1} = ['X: ', num2str(pos(1))];
output_txt{2} = ['Y: ', num2str(pos(2))]; %this is the text next to the cursor
end
那么你的位置是pos
你可以添加%Some code to add the [x y] to the list box
again
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)