Listener not responding to ui slider

I didn't like how the normal callback implementation on the slider only updated once I stop dragging it, so I tried addlistener instead, but for some reason it just doesn't respond at all. As a simple example, for the following code
sld = uicontrol('Style','slider');
lst = addlistener(sld,'Value','PostSet',@(src,evt)disp('value changed'));
Changing the value of the slider directly from the Command Window ( sld.Value=0.5; ) causes the callback function to run, but moving the slider or pressing the arrows on its ends does nothing.
What am I doing wrong?

4 Comments

Works for me in R2022a.
That is, running your code in R2022a causes 'value changed' to be displayed on the command line when changing the slider's value in any way: clicking/holding down the arrows, dragging the bar, clicking/holding down the space between the bar and the arrow, setting sld.Value programmatically.
It also works for me in 2024b.
Interestingly, however, it does not work in 2025a prerelease (update 3). I see exactly the behavior described by the OP.
@Sebastian: if you are using 2025a prerelease, you should click the Feedback button in the RESOURCES section of the toolstrip and report the problem to Mathworks. If you are not using the prerelease let me know and I can report that issue (which of course, won't really help you if you are really using 2024b).
@Les Beckham, I'm on R2024b. I'm also running on LinuxMint if that helps at all - I've had a couple specific and weird issues in the past that I'm pretty sure were Linux-specific.
Ah yes, that could be a difference as well. I'm on Windows 10. I'll report the issue with 2025a. Sorry to not have any more ideas to help you.

Sign in to comment.

Answers (1)

If you use AppDesigner then you can use the "Value changing" event to do an instantaneous/live updating of the displayed value. For example, here is an example from one of my programs:
% Value changing function: sldOpacity
function sldOpacityValueChanging(app, event)
changingValue = event.Value;
tt = sprintf('%.1f %% original and %.1f %% colorized', changingValue, 100-changingValue);
app.lblOpacity.Text = tt; % Change static text label above the slider.
app.lblOpacity.Tooltip = tt;
end

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Release

R2024b

Tags

Asked:

on 20 Mar 2025

Answered:

on 21 Mar 2025

Community Treasure Hunt

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

Start Hunting!