如何在Matlab中在线单击NOT的反应



我有一个密集的Matlab图与许多曲线,我希望用户能够突出显示其中一条线(除了只是看到它的数据提示)。它工作得很好,但是我找不到一种方法来"取消选择"。我找不到一个可以分配"取消选择"的操作。函数。我试着添加一个专用的文本,并分配给它的ButtonDownFcn,但它似乎没有被调用。理想情况下,我希望使用"取消选择"函数只在单击空格时调用(不是在任何曲线上),但是使用额外文本的变通方法也可以工作。

兆瓦:

clear
close all
p(1) = plot(0:100, sin(0:100));
hold on
p(2) = plot(0:100, cos(0:100));
legend('sin', 'cos')
ylim([-2, 2])
% Attach a method to clicking a plot line
datacursormode on;
dcmgr = datacursormode(gcf);
set(dcmgr, 'UpdateFcn', @select)

function output_txt = select(~, event_obj, ~)
% change tooltip
pos = get(event_obj, 'Position');
output_txt = {...
[event_obj.Target.DisplayName]...
num2str(pos(1), 4)...
num2str(pos(2), 4) ...
};
% reset line widths, make them semi-transparent
p = findall(gcf, 'type', 'line');
set(p, 'LineWidth', 1);
for i = 1:numel(p)
p(i).Color = [p(i).Color, 0.2];
end
% set selected line width to 2 and colour to fully opaque
event_obj.Target.LineWidth = 2;
event_obj.Target.Color = [event_obj.Target.Color, 1];
% create a "clear selection" object --> THIS PART DOESN'T SEEM TO WORK
t = text(0, 0, 'clear');
set(t, 'ButtonDownFcn', @unselect)
end
function unselect
p = findall(gcf, 'type', 'line');
set(p, 'LineWidth', 1);
for i = 1:numel(p)
p(i).Color = [p(i).Color, 1];
end
end

我认为这里的问题是Matlab在启用datacursormode时覆盖ButtonDownFcn侦听器(缩放,平移和rotate3d)。如果您关闭datacursormode,ButtonDownFcn将工作。

关于此行为的更多信息在这里:Matlab Answers

我无法让按钮向下功能工作,但以下代码使用按键作为事件在R2019a上工作。我使用的是c键,但你可以使用任何对你的情况最有意义的。

clear
close all
F = figure();
hManager = uigetmodemanager(F);
p(1) = plot(0:100, sin(0:100));
hold on
p(2) = plot(0:100, cos(0:100));
legend('sin', 'cos')
ylim([-2, 2])
% Attach a method to clicking a plot line
dcmgr = datacursormode(gcf);
datacursormode on;
hManager.WindowListenerHandles(1).Enabled = false;
hManager.WindowListenerHandles(2).Enabled = false;
set(dcmgr, 'UpdateFcn', @select)
set(F, 'KeyPressFcn', @unselect)
function output_txt = select(~, event_obj, ~)
% change tooltip
pos = get(event_obj, 'Position');
output_txt = {...
[event_obj.Target.DisplayName]...
num2str(pos(1), 4)...
num2str(pos(2), 4) ...
};
% reset line widths, make them semi-transparent
p = findall(gcf, 'type', 'line');
set(p, 'LineWidth', 1);
for i = 1:numel(p)
p(i).Color = [p(i).Color, 0.2];
end
% set selected line width to 2 and colour to fully opaque
event_obj.Target.LineWidth = 2;
event_obj.Target.Color = [event_obj.Target.Color, 1];
end

function unselect(fig, event_obj)
if strcmpi(class(event_obj),'matlab.ui.eventdata.KeyData') && ...
~isempty(event_obj.Character) && ...
event_obj.Character == 'c'

p = findall(gcf, 'type', 'line');
set(p, 'LineWidth', 1);
for i = 1:numel(p)
p(i).Color = [p(i).Color, 1];
end

% Delete data tips
figObjs = findall(fig);
pdth = figObjs(arrayfun(@(h)isa(h,'matlab.graphics.shape.internal.PointDataTip'),figObjs));
for idx = 1:numel(pdth)
delete(pdth(idx))
end
end
end

相关内容

  • 没有找到相关文章

最新更新