필터 지우기
필터 지우기

Avoid deleting of datatip when clicking on marker

조회 수: 4 (최근 30일)
Sebastian Hölz
Sebastian Hölz 2024년 6월 6일
답변: Sebastian Hölz 2024년 6월 12일
When creating a datatip DT, it is deleted when I click on the marker without moving the DT.
Example:
figure
h = plot(rand(100,1));
DT = datatip(h,'DataIndex',22);
Now, clicking on the marker of the DT without moving this, will delete the DT when releasing the mouse-button. Important: I still want to be able to move the DT, it should just not be deleted when clicking on it without moving it.
Is there a simple way of preventing this from happening? I implemented a solution using listeners and undocumented stuff, but I was wondering if there is a simple solution that I'm just missing.

채택된 답변

Sebastian Hölz
Sebastian Hölz 2024년 6월 12일
OK, here my solution which allows to move the DT, allows deleting the DT via contextmenu and restores it when being deleted by clicking on the DT. I don't quite like the fact that I cannot avoid the DT from being destroyed and have to restore it. Also, checking the stack to allow for deleting when being called by the contexmenu is a hack which might not work sometimes in the future. So, if anyone finds a more documented solution, I would be glad to hear it.
function DataTip_test
figure
h = plot(rand(100,1));
DT = datatip(h,'DataIndex',22);
addlistener(DT, 'ObjectBeingDestroyed',@RestoreDT);
% ==========================
function RestoreDT(varargin)
% Allow deleting when parent line is being deleted
if strcmp(h.BeingDeleted,'on'); return; end
% Allow deleting, if triggered by context menu
db = dbstack;
if strcmp(db(end).file,'createContextMenu.p'); return; end
% Otherwise, restore datatip
DT = datatip(h, 'DataIndex',DT.DataIndex);
addlistener(DT, 'ObjectBeingDestroyed',@RestoreDT);
end
end

추가 답변 (1개)

Matlab Pro
Matlab Pro 2024년 6월 6일
Just set the 'PickableParts' = 'none'
DT = datatip(h,'DataIndex',22,'PickableParts','none');
  댓글 수: 1
Sebastian Hölz
Sebastian Hölz 2024년 6월 7일
편집: Sebastian Hölz 2024년 6월 10일
My original question was not quite complete. I still want to be able to drag the DT to a different position. It should just not be deleted when I click on it without dragging it. Thus, the above answer does not solve my problem.
Sorry for not being precise enough, I have updated the original question to mention this.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Code Execution에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by