1

I'm working on a continuously updating slider...

hsl = uicontrol(...); % Slider initializing stuff
vars=struct('hsl', hsl, 'x', x, 'y', y); % A bunch of stuff that my callback needs
set(hsl,'Callback',{@hsl_callback,vars});

addlistener(hsl,'ContinuousValueChange',@hsl_callback);

Here's the problem. If I leave it like that, I get the error "not enough input arguments" for the callback.

If I change the line to this:

addlistener(hsl,'ContinuousValueChange',@(vars)hsl_callback);

then I get the error "too many input arguments."

Is this not possible, or am I getting the syntax wrong?

If it helps, my callback function has this structure:

function hsl_callback(~,~,vars)
    k = get(vars.hsl,'Value');
    % plot x, y scaled by k
end
3
  • try addlistener(hsl,'ContinuousValueChange',@() hsl_callback(vars));? Commented Aug 10, 2015 at 21:25
  • I just did, but it still says that there are too many input arguments. Commented Aug 10, 2015 at 22:39
  • I have also tried addlistener(hsl,'ContinuousValueChange',@{hsl_callback,vars});, which doesn't work either. Commented Aug 10, 2015 at 23:43

1 Answer 1

2

First, I would avoid using vars to store your data. The slider handle can be passed directly in your callback, and x and y may be stored in the UserData property. This gives you the ability to change x and y dynamically, if needed. Change your callback method declaration to:

function hsl_callback(hObject,eventdata)
  % Retrieve k and vars
  k = get ( hObject , 'Value' );
  vars = get ( hObject , 'UserData' );

  % Plot x,y scaled by k
  ...
end

Then, I would change slider creation to:

% Define x and y.
vars = struct('x',x,'y',y);

% Create slider, assigning vars to UserData.
hSlider = uicontrol('Sytle','Slider',...,'UserData',vars);

% Assign the callback method, executed whenever the slider is released.
set(hSlider,'Callback',@hsl_callback);

% Assign the listener, executing whenever the slider value changes.
hListener = addlistener(hSlider,'ContinuousValueChange',@(src,eventdata)hsl_callback);

Depending on your version, you may have to use this instead:

hListener = addlistener ( hSlider , 'Value' , 'PostSet' , @(src,eventdata)hsl_callback );
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the suggestion, this seems like a better way to handle passing in data. However, I'm still getting the same error on the addlistener line- not enough input arguments.
Oops, this is the working code: hListener = addlistener(hSlider,'ContinuousValueChange',@hsl_callback);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.