"Unable to update data tip using custom update function"

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

 Akzeptierte Antwort

Stephen23
Stephen23 am 1 Dez. 2025
Bearbeitet: Stephen23 am 1 Dez. 2025
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 Kommentare

Sorry but where does
info.Position
fit into the code?
Stephen23
Stephen23 am 1 Dez. 2025
Bearbeitet: Stephen23 am 2 Dez. 2025
"Sorry but where does info.Position fit into the code?"
Most likely inside displayteam (i.e. where you write the code that determines what to display). For example:
function txt = displayteam(~,info,data3)
idx = info.DataIndex; % index of the clicked point
fnm = data3.Franchise{idx}; % curly brace indexing
wpc = data3.Wins(idx) ./ (data3.Wins(idx) + data3.Losses(idx));
cnt = data3.Championships(idx);
txt = sprintf("(%s, %g, %d)", fnm, wpc, cnt);
end
I see, but what is the method to get the index of the clicked point?
Stephen23
Stephen23 am 2 Dez. 2025
Bearbeitet: Stephen23 am 2 Dez. 2025
"I see, but what is the method to get the index of the clicked point?"
From the 2nd input of the UpdateFcn callback function:
info.DataIndex
Its value is generated by MATLAB when you click on a point. You just need to use it:
function txt = displayteam(~,info,data3)
idx = info.DataIndex; % index of the clicked point
fnm = data3.Franchise{idx}; % curly brace indexing
wpc = data3.Wins(idx) ./ (data3.Wins(idx) + data3.Losses(idx));
cnt = data3.Championships(idx);
txt = sprintf("(%s, %g, %d)", fnm, wpc, cnt);
end
Ayaan
Ayaan am 2 Dez. 2025
Bearbeitet: Ayaan am 2 Dez. 2025
The code is working and it is showing what i wanted but it is still coming up with an error?
Here is the new code for the plot
data2 = readtable("alltimeteams.xlsx",'VariableNamingRule','preserve');
hold on
winPercent = (data2.Wins ./ (data2.Wins + data2.Losses)); % Calculate the win percentage for each tea
plot(winPercent, data2.Championships,"LineStyle","none","Color","r","Marker","*"); % plots graph of championships against total win percentage
xlabel('Win Percentage');
ylabel('Championships');
title('Team performance since the beginning of the NBA');
grid on;
set(gcf,'Position',[50,500,700,500])
dcm = datacursormode;
dcm.Enable = 'on'; % switches on datacursormode
dcm.UpdateFcn = {@displayteam,data2};
displayteam(data2);
getCursorInfo(dcm);
and the code for the function is the same as yours.
Here is the error
Not enough input arguments.
Error in displayteam (line 2)
idx = info.DataIndex; % index of the clicked point
^^^^^^^^^^^^^^
Error in WinChampgraph_team_selector (line 16)
displayteam(data2);
^^^^^^^^^^^^^^^^^^
Error in Main_script (line 16)
WinChampgraph_team_selector()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I think it is something to do with calling the fucntion but i cant figure out what
Stephen23
Stephen23 am 2 Dez. 2025
Bearbeitet: Stephen23 am 2 Dez. 2025
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.
Great, thank you!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Line Plots finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 30 Nov. 2025

Kommentiert:

am 2 Dez. 2025

Community Treasure Hunt

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

Start Hunting!

Translated by