"Unable to update data tip using custom update function"

51 views (last 30 days)
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

Accepted Answer

Stephen23
Stephen23 on 1 Dec 2025 at 5:53
Edited: Stephen23 on 1 Dec 2025 at 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 Comments
Stephen23
Stephen23 on 2 Dec 2025 at 16:08
Edited: Stephen23 on 2 Dec 2025 at 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!