Trouble with custom data tip function (event)

24 views (last 30 days)
I am puzzled? I try to have a line drawn during the call to a custom text data tip function.
Here is the "normal" response, when clicking on a point with the mouse :
Here's the error message I get when I try to do the same mouse click while adding a few extra lines of code in the callback (first, a screen capture of the message, followed by the code :
Here is the code :
function txt = displayCoordinates(~, info)
global clickedCIE_L clickedCIE_a clickedCIE_b; % sorry for the globals abuse...
clickedCIE_L = info.Position(3); % 69
clickedCIE_a = info.Position(1); % -56
clickedCIE_b = info.Position(2); % 46
% handle_ligne = line([0 0],[clickedCIE_a clickedCIE_b],[0 0],'color',[0 0 1],'lineWidth',4);
DisplayAngle(clickedCIE_a, clickedCIE_b); <<< --------------------- This statement is causing the error?
Hrad = mod(atan2(clickedCIE_b,clickedCIE_a),2*pi); % 2.4539
hueAngle = Hrad*180/pi; % 140 degrés
chroma = sqrt(clickedCIE_a.^2 + clickedCIE_b.^2); % 72
txt = {sprintf('CIE L: %.0f', info.Position(3)), sprintf('CIE a: %.0f', info.Position(1)), sprintf('CIE b: %.0f', info.Position(2)), sprintf('CIE c : %.0f ', chroma), sprintf('CIE h : %.0f °', hueAngle) };
end
function DisplayAngle(a,b)
global clickedCIE_L clickedCIE_a clickedCIE_b;
a_start = 0;
b_start = 0;
a_end = clickedCIE_a
b_end = clickedCIE_b
% Delete existing marker, if any
if ~isempty(handle_ligne)
delete(handle_ligne);
end
handle_ligne = line([a_start a_end],[b_start b_end],[0 0],'color',[0.2 0.2 0.2],'lineWidth',4);
end
It does not matter whether I put the DisplayAngle function inline of not, I still get the error message.
Would there be any other approaches? All I'm trying to add to the figure is a line depicting the hue angle and chroma, at the bottom.
I'll continue looking around for the error on my own in case I find any solution... And I appreciate your precious help.

Answers (1)

Infinite_king
Infinite_king on 8 Jan 2024
Hi Roger Breton,
I understand the you are using custom function to update the data tip text, however this was resulting in the 'unable to update the data tip using custom function' error.
I was able to reproduce the error on my end, and with a few changes to the callback function, I was able to get rid of the error. Make the following changes to the callback function to resolve the error,
  • Write all the operations in one single callback function. In this case, instead of calling the 'DisplayAngle' function from the 'displayCoordinates' function, insert the operations directly into the callback function itself. For example, the following function works correctly when used as a custom data tip callback function,
function txt = displayCoordinates(~, info)
global clickedCIE_L clickedCIE_a clickedCIE_b; % sorry for the globals abuse...
clickedCIE_L = info.Position(3); % 69
clickedCIE_a = info.Position(1); % -56
clickedCIE_b = info.Position(2); % 46
% insert the required operations here itself
handle_ligne = line([0 0],[clickedCIE_a clickedCIE_b],[0 0],'color',[0 0 1],'lineWidth',4);
% handle_ligne = DisplayAngle(clickedCIE_a, clickedCIE_b);
Hrad = mod(atan2(clickedCIE_b,clickedCIE_a),2*pi); % 2.4539
hueAngle = Hrad*180/pi; % 140 degrés
chroma = sqrt(clickedCIE_a.^2 + clickedCIE_b.^2); % 72
txt = {sprintf('CIE L: %.0f', info.Position(3)), sprintf('CIE a: %.0f', info.Position(1)), sprintf('CIE b: %.0f', info.Position(2)), sprintf('CIE c : %.0f ', chroma), sprintf('CIE h : %.0f °', hueAngle) };
end
  • It seems like you are encountering an issue related to the scope of the 'handle_ligne' variable in your 'DisplayAngle function'. Make sure the variable is accessible outside the function. For example,
function DisplayAngle(a, b)
global handle_ligne; % making it global
a_start = 0;
b_start = 0;
a_end = a;
b_end = b;
% Delete existing marker, if any
if ~isempty(handle_ligne) && isvalid(handle_ligne)
delete(handle_ligne);
end
% Define handle_ligne in the global scope
handle_ligne = line([a_start a_end], [b_start b_end], [0 0], 'color', [0.2 0.2 0.2], 'lineWidth', 4);
end
  • Save the 'displayCoordinates.m' file in the location that is on the MATLAB path. Alternatively, you can place this function in the same script file.
  • Make sure the 'UpdateFcn' property was set correctly.
dcm = datacursormode;
dcm.Enable = 'on';
dcm.UpdateFcn = @displayCoordinates;
For more information, refer the following MATLAB documentation,
Hope this is helpful.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!