MATLAB: How do i make function handles work

callbackfunction handles

Hello, I have a really annoying error that i can't find a way to make it work, my code looks like this:
function [h] = test_listeners()
f = figure('Units','Normalized');
h.Red = uicontrol('Parent',f,...
'Units','Normalized',...
'Style','slider',...
'position',[0.1, 0.6, 0.3, 0.1],...
'backgroundColor',[1,0.85,0.85],...
'FontSize',15);
h.Blue = uicontrol('Parent',f,...
'Units','Normalized',...
'Style','text',...
'position',[0.1, 0.3, 0.3, 0.1],...
'backgroundColor',[0.85,0.85,1],...
'FontSize',15);
addlistener(h.Red,'Action',@()callbackBlue(h.Blue));
end
function [] = callbackBlue(varargin)
disp(varargin)
end
in one sentence it should change the value of BLUE according to the value of RED, but even though in input of the callback is varargin when running the code i get this error message:
Warning: Error occurred while executing the
listener callback for event Action defined
for class matlab.ui.control.UIControl:
Error using
test_listeners>@()callbackBlue(h.Blue)
Too many input arguments.
I am currently stuck and will love some help.

Best Answer

addlistener(h.Red, 'Action', @(varargin)callbackBlue(varargin{:}, h.Blue));
Remember, listeners have two arguments automatically added, just like all other callbacks (except for callbacks that are acting as filters, such as zoom constraint functions.)