datacursormode: how to permanently turn on "make new data tip"?

8 visualizaciones (últimos 30 días)
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 comentario
Adam
Adam el 14 de Nov. de 2017
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!

Iniciar sesión para comentar.

Respuesta aceptada

Yair Altman
Yair Altman el 15 de Nov. de 2017
Although the official documentation of the datacursormode function says explicitly that:
"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 comentarios
Marko Usaj
Marko Usaj el 15 de Nov. de 2017
Dear Yair, you are the best! Works like a charm! You also solved my next big wish to change this nasty black square as a data tip to something else. Empty circles are just perfect!
However, my next goal is to be able to call this function from some simple GUI, which enable switching between different functions (zoom, pan, get data, save data...) using HotKeys. I tried equivalent approach as I have it before but it seems that the function cannot be activated in the same way. If you have any more tips to give me here I would really appreciate. Here is the code:
1. GUI with some plots
function HotKeyAcc
global hLine;
% A figure
fig = figure('menubar','none');
% Add menus with Accelerators
mymenu = uimenu('Parent',fig,'Label','Hot Keys');
uimenu('Parent',mymenu,'Label','Zoom','Accelerator','w','Callback',@(src,evt)zoom(fig,'on'));
uimenu('Parent',mymenu,'Label','Pan','Accelerator','x','Callback',@(src,evt)pan(fig,'on'));
uimenu('Parent',mymenu,'Label','GetPoints','Accelerator','d','Callback',@(src,evt)getDataFromFigureNew(hLine));
%uimenu('Parent',mymenu,'Label','Data','Accelerator','a','Callback',@(src,evt)loadNextTrace(fig,totint,totintbg));
%uimenu('Parent',mymenu,'Label','Data','Accelerator','s','Callback',@(src,evt)SaveDataPoints(fig));
%some plots
a = -16; t = 0:60;
ha(1) = subplot(2,1,1);
hLine=plot(t,sin(a*t));
ha(2) = subplot(2,1,2);
plot(t,sin(5*a*t));
linkaxes(ha, 'x');
end
2. Your function I want to call to pick the data points
function getDataFromFigureNew(hLine)
set(hLine, 'ButtonDownFcn', @lineClickedCallback,'enable', 'on');
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);
set(cursorMode,'UpdateFcn',@myUpdateFcn);
% 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
function txt = myUpdateFcn(~, ~)
txt = {''};% I do not want any info about data points displayed on figure
end
end
3. This is my old function to get data points which worked with this approach:
function getDataFromFigure2(fig)
dcm_obj = datacursormode(fig);
set(dcm_obj,'UpdateFcn',@myUpdateFcn,'enable', 'on');
function txt = myUpdateFcn(~, event_obj)
% no data shown on figure
txt = {''};
end
end
Yair Altman
Yair Altman el 19 de Nov. de 2017
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Graphics Performance en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by