"Unable to update data tip using custom update function"

51 visualizaciones (últimos 30 días)
Ayaan
Ayaan el 30 de Nov. de 2025 a las 23:25
Comentada: Ayaan el 2 de Dic. de 2025 a las 16:33
Hey guys, when I make the custom text function for dataCursorMode it keeps on showing this when clicking on the marker on the plot
Here is the code for the plot
function WinChampgraph_team_selector
data2 = readtable("alltimeteams.xlsx",'VariableNamingRule','preserve');
% Calculate the win percentage for each team
winPercent = (data2.Wins ./ (data2.Wins + data2.Losses));
hold on
graph = plot(winPercent, data2.Championships,"LineStyle","none","Color","r","Marker","*");
dcm = datacursormode;
dcm.Enable = 'on';
dcm.UpdateFcn = @displayteam;
getCursorInfo(dcm);
displayteam(data2);
xlabel('Win Percentage');
ylabel('Championships');
title('Team performance since the beginning of the NBA');
grid on;
set(gcf,'Position',[50,500,700,500])
end
Here is the code for the custom text function
function txt = displayteam(data3)
x = data3.Franchise;
y = (data3.Wins ./ (data3.Wins + data3.Losses));
myDatatipText = "(%s, %s, %s)";
txt = sprintf(myDatatipText, string(x), string(y),data3.Championships);
end
Then it shows this on the plot
Unable to update data tip using custom update function
Please lmk is anything else is needed

Respuesta aceptada

Stephen23
Stephen23 el 1 de Dic. de 2025 a las 5:53
Editada: Stephen23 el 1 de Dic. de 2025 a las 19:49
Solution One
Replace
dcm.UpdateFcn = @displayteam;
with
dcm.UpdateFcn = @(~,~)displayteam(data2);
Explanation
The property UpdateFcn is called with two input arguments, as its documentation explains:
Instead of following the documentation you have provided one input argument in the function signature, which is a table (and not either of the inputs specified in the UpdateFcn documentation). So to provide your desired input table we can simply define the UpdateFcn as an anonymous function which accepts (and ignores) the two documented inputs and passes your table:
Solution Two
You will then notice that every data tip is exactly the same. That is because the displayteam function has no way to distinguish what data point was clicked on. So if you want the datatips to be different then you need to modify displayteam to e.g. also include the info structure...
For example use
dcm.UpdateFcn = {@displayteam,data2};
and define the function to accept all three inputs:
function txt = displayteam(~,info,data3)
and inside displayteam you can then use the values of
info.Position
and/or
info.Index
to match to your imported table data. As I have no idea what logic you want to implement I will leave that up to you.
  7 comentarios
Stephen23
Stephen23 el 2 de Dic. de 2025 a las 16:08
Editada: Stephen23 el 2 de Dic. de 2025 a las 16:18
Delete this line:
displayteam(data2);
In my lastest code here
the function signature was modified to require three input arguments. However on that line you call it with one input only, thus the error. Note that the line returns a text output which you ignore and discard, so that line is completely superfluous anyway. Simplest solution: get rid of that line.
Ayaan
Ayaan el 2 de Dic. de 2025 a las 16:33
Great, thank you!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Line Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by