datacursormode: how to display info about current data point in relation to all data points on the same figure?

10 visualizaciones (últimos 30 días)
Hello! I am selecting multiple data points on the same figure using Data Cursor (Shift+Click). I would like that UpdateFcn displays if last selected data point (for example) has pair (i.e. if total number of data points is even). I have made some code but all I get is "unable to update data tip using custom update function". Thank you for any comments.
function getDataFromFigure1
fig = figure;
a = -16; t = 0:60;
plot(t,sin(a*t));
dcm_obj = datacursormode(fig);
set(dcm_obj,'UpdateFcn',@myUpdateFcn,'enable', 'on');
function txt = myUpdateFcn(~, event_obj)
% read out all data points
mypoints = getCursorInfo(dcm_obj);
p=cell2mat({mypoints.Position}');
N=size(pos(:,1),1);
if mod(N,2)==0
txt = {'this data point does not have pair'};
else
txt = {'this data point has pair'};
end
end
end

Respuestas (1)

Yair Altman
Yair Altman el 20 de Nov. de 2017
You can get the clicked line object from event_obj.Target, and from there get the corresponding data points for the entire line:
function txt = myUpdateFcn(hDataTip, event_obj)
% Get the clicked data coordinates
clickedPoint = hDataTip.Position; % [x,y,z] array
% Now get all the data points on the clicked line
hLine = event_obj.Target; % or: hDataTip.DataSource or: hDataTip.Host
xData = hLine.XData; % 1xN array
yData = hLine.YData; % 1xN array
zData = hLine.ZData; % 1xN array
... % now do something useful with all this info
end
You can easily modify the code above to compare the clicked data point's coordinates to all the other data-points in the clicked line.
  2 comentarios
David Rankin
David Rankin el 21 de En. de 2018
Editada: David Rankin el 21 de En. de 2018
edited with solution that I found:
I have a similar problem but this code seems to solve it. I look at the Tag property to determine which line has been clicked on. Previously I was using IF statements and it was causing the error message.
However, the OTHERWISE case doesn't work as expected. If a plot is passed in without a tag named in the list, it returns an error message instead of the message in the OTHERWISE case. I would love to find out why this is happening but for now I will just make sure I cover the cases.
Here is my code:
function txt = datatiptxt(~, hDatatip)
pos = get(hDatatip, 'Position');
t = get(hDatatip.Target, 'Tag');
switch (t)
case 'Temperature'
val = pos(2);
str = [num2str(val), ' C'];
txt = {['Time: ', num2str(pos(1))], str};
case 'Humidity'
val = pos(2);
str = [num2str(val), ' pct'];
txt = {['Time: ', num2str(pos(1))], str};
otherwise
val = pos(2);
txt = {["datatip: ', num2str(pos(1)), str);
end
end
Fritz
Fritz el 17 de Ag. de 2018
Hi David
Error and warning messages do not seem to work inside the datatip update function.
And there is a syntax error in the second line of your OTHERWISE statement.
Should be:
{['datatip: ', num2str(pos(1))], str};
Kind regards

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing 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