datacursormode: how to permanently turn on "make new data tip"?
4 次查看(过去 30 天)
显示 更早的评论
Hello! I want to select multiple data points on figure using data cursor (shift + click). Can this functionality be turned on programmatically in a way that there is no need for holding shift? Thank you for any advice!
1 个评论
Adam
2017-11-14
Not that I am aware of.
doc datacursormode
gives details of what you can change programmatically on the datacursormode object, but this is not one of the things and I don't imagine it would be anywhere else. It may be possible with underlying java programming, but I wouldn't have a clue about that!
采纳的回答
Yair Altman
2017-11-15
"You place data tips only by clicking data objects on graphs. You cannot place them programmatically (by executing code to position a data cursor)."
This is in fact WRONG AND MISLEADING, as I explained far back in 2011: https://undocumentedmatlab.com/blog/controlling-plot-data-tips
The general solution for your specific request:
1. First, set the plot line's button-down function to a custom callback function:
hLine = plot(...);
set(hLine, 'ButtonDownFcn', @lineClickedCallback);
2. Next, implement this callback function that will add the requested data-tip at the clicked location:
function lineClickedCallback(hLine, eventData)
% Get the clicked position from the event-data
pos = eventData.IntersectionPoint;
% Get the line's containing figure
hFig = ancestor(hLine,'figure');
% Get the figure's datacursormode object
cursorMode = datacursormode(hFig);
% Create a new data-tip at the clicked location
hDatatip = cursorMode.createDatatip(hLine);
set(hDatatip, 'Position',pos, 'MarkerSize',5, 'MarkerFaceColor','none', 'MarkerEdgeColor','r', 'Marker','o', 'HitTest','off');
end
You can modify this code to make the data-tip and/or marker look different.
2 个评论
Yair Altman
2017-11-19
This is an altogether different question so you should accept the answer on this thread (if as you said it answered it), and then open a new thread for this new question (with an appropriate title and description).
In the new question, if you make your code simpler, it is more likely to be answered: People answer questions on this forum in their spare time and don't typically have time to read lengthy codes posted by others.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!