Continuous slider callback

Every few months, a CSSM forum reader asks how to set up a continuously-invoked slider callback: Matlab’s slider uicontrol invokes the user callback only when the mouse button is released, and not continuously while the slider’s thumb is dragged. This functionality was again referred-to yesterday, and I decided it merits a dedicated post.

There are three distinct simple ways to achieve continuous callbacks:

Using Java callbacks

As explained in an earlier article, Matlab uicontrols are basically Java Swing objects that possess a large number of useful callbacks. Matlab sliders’ underlying Java objects, which are really not JSliders but JScrollBars, have an AdjustmentValueChangedCallback property that is useful for our purposes and is accessible using the FindJObj utility. Simply download FindJObj from the File Exchange, and then:

hSlider = uicontrol('style','slider', ...);
jScrollBar = findjobj(hSlider);
jScrollBar.AdjustmentValueChangedCallback = @myCbFcn;
% or: set(jScrollBar,'AdjustmentValueChangedCallback',@myCbFcn)

Where myCbFcn is the Matlab callback function that will be invoked continuously when the arrow buttons are depressed or the slider’s thumb is dragged.

Using an event listener

An alternative to the Java route is to use Matlab’s undocumented handle.listener function to listen to the slider’s Action event, as follows:

hListener = handle.listener(hSlider,'ActionEvent',@myCbFcn);
setappdata(hSlider,'sliderListener',hListener);  % this is important - read below

This alternative is used by Matlab’s own imscrollpanel function:

if isJavaFigure
   % Must use these ActionEvents to get continuous events fired as slider
   % thumb is dragged. Regular callbacks on sliders give only one event
   % when the thumb is released.
   hSliderHorListener = handle.listener(hSliderHor,...
      'ActionEvent',@scrollHorizontal);
   hSliderVerListener = handle.listener(hSliderVer,...
      'ActionEvent',@scrollVertical);
   setappdata(hScrollpanel,'sliderListeners',...
      [hSliderHorListener hSliderVerListener]);
else
   % Unfortunately, the event route is only available with Java Figures,
   % so platforms without Java Figure support get discrete events only
   % when the mouse is released from dragging the slider thumb.
   set(hSliderHor,'callback',@scrollHorizontal)
   set(hSliderVer,'callback',@scrollVertical)
end

In this case, hScrollpanel is merely a handle to a panel in Matlab’s imscrollpanel code. You can use any Matlab control to store the listener handle, including hSlider itself, which would be simplest. It doesn’t matter where or how exactly you store hListener, since you will not use it directly in your program. The important thing is just to store it *anywhere*, so that it remains in persistent (heap) memory. As long as the reference handle is “alive”, the listener will keep working. This is explained here.

Using a property listener

The handle.listener function can also be used to listen to property value changes. In our case, set a post-set listener, that gets triggered immediately following Value property updates, as follows:

hhSlider = handle(hSlider);
hProp = findprop(hhSlider,'Value');  % a schema.prop object
hListener = handle.listener(hhSlider,hProp,'PropertyPostSet',@myCbFcn);
setappdata(hSlider,'sliderListener',hListener);  % this is important - read above

In addition to ‘PropertyPostSet’, we could also listen on ‘PropertyPreSet’, which is triggered immediately before the property is modified. There are also corresponding ‘*Get’ options. In relatively old Matlab releases (I believe R2007b and earlier, but I’m not certain), the option names were simply ‘PostSet’, ‘PreSet’ etc., without the ‘Property’ prefix.

Do you know of any other way to achieve continuous callbacks? If so, I would be delighted to hear in the comments section below.

Related posts:

  1. Controlling callback re-entrancy Callback reentrancy is a major problem for frequently-fired events. Luckily, it can easily be solved....
  2. Inactive Control Tooltips & Event Chaining Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....
  3. Setting listbox mouse actions Matlab listbox uicontrol can be modified to detect mouse events for right-click context menus, dynamic tooltips etc....
  4. Editbox data input validation Undocumented features of Matlab editbox uicontrols enable immediate user-input data validation...
  5. UISplitPane UISplitPane was recently chosen as Matlab Central's Pick of the Week. Here I detail its use of some undocumented Matlab features....
  6. Uicontrol callbacks This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...

Categories: GUI, Java, Listeners, Medium risk of breaking in future versions, UI controls

Tags: , , , , , , ,

Bookmark and SharePrint Print

38 Responses to Continuous slider callback

  1. Dani says:

    Hi,

    another functionality that would benefit from continuous callback is figure resizing. For example, it would be useful to allow for figure resizing, but only to a certain minimum size. Since ResizeFcn callback is only evaluated once the mouse button is released, ‘jumping’ from, for example, very small resized windows to the desired minimum size cannot be avoided.

    The other problem with figure sizes and resizing is that Matlab only knows the original screen size. This leads problems if the screen resolution has been changed on the way, for example due to connection to a projector.

    Can these be solved with some java magic?

    Dani

    • @Dani – Unfortunately, the window resizing event is only propagated upon mouse release. There’s probably a Java workaround, but I don’t know it. You can use the three separate methods shown above, but you’ll see the same result:

      % 1. Callbacks:
      jFrame = get(gcf,'JavaFrame');
      jhFrame = handle(jFrame.fFigureClient.getWindow, 'CallbackProperties');
      set(jhFrame,'ComponentResizedCallback',@myCallback);
       
      % 2. Event listener:
      hListener = handle.listener(gcf,'ResizeEvent',@myCallback);
       
      % 3. Property listener:
      hFig = handle(gcf);
      hProp = findprop(hFig,'Position');
      hListener = handle.listener(hFig,hProp,'PropertyPostSet',@myCallback);
    • Matt Whitaker says:

      For the screensize issue try:

      import java.awt.*;
      toolkit = Toolkit.getDefaultToolkit();
      scrnsize = toolkit.getScreenSize();
      h= scrnsize.getHeight;
      w =scrnsize.getWidth;

      Not sure if there is some kind of listener for changes in that

    • The screensize can also be gotten via pure Matlab:

      pos = get(0,'ScreenSize');

      Note that if you have multiple concurrent monitors this gets a bit trickier.

    • Dani says:

      You guys are worth gold! (German proverb, not sure it exists in English)

      @Yair – thanks, good to know it cannot be done.

      @Matt – excellent, works and detects changes in screen resolution.

    • Dani says:

      @Yair – unfortunately

      get(0,'ScreenSize');

      does not (reliably) return updated screen resolution info in cases where the resolution was changed during a running MATLAB session. However, now you got me worried regarding multiple monitors and I had a look at

      get(0,'MonitorPositions')

      which does seem to return correctly updated info.

    • To be precise, I didn’t say it cannot be done, only that I personally have not found a way to do it. I actually tend to think it can be done somehow.

  2. Gerrit says:

    Hi,
    I recently implemented a GUI with AdjustmentValueChangedCallback for a JTree’s vertical slider. The acquired values were used to update the slider position of one or more dependent listboxes. (The tree shows a directory with all subdirectories and files, the listboxes only display one file type each, but in the same rows as they are in the tree.)
    Problem is that if the JTree’s slider position changed fast (e.g. via mouse wheel usage), strings inside the listboxes are not in their correct positions or displayed twice.

    mylb1 = uicontrol('Style','listbox');
    mylb2 = uicontrol('Style','listbox');
    mytree = uitree;
    myjtree = mytree.getTree;
    %
    myjscrollpane = findjobj(gcf,'property',{'VerticalScrollBarPolicy',20});
    dummy = myjscrollpane.getComponents;
    myjslider = dummy(2);
    %
    mylbscrollpanes = findjobj(gcf,'property',{'VerticalScrollBarPolicy',22});
    dummy1 = mylbscrollpanes(1).getComponents;
    dummy2 = mylbscrollpanes(2).getComponents;
    mylb1slider = dummy1(2);
    mylb2slider = dummy2(2);
    %
    set(myjslider,'AdjustmentValueChangedCallback',{@myAdjustFcn});
    set(myjtree,'MousePressedCallback',{@myMouseFcn},...
    'TreeExpandedCallback',{@myExpFcn},...
    'TreeCollapsedCallback',{@myColFcn});

    The listbox strings are updated via “myExpFcn” and “myColFcn” and contain correct data.
    “myAdjustFcn” gets myjslider’s “Maximum” and “Value” and sets them in mylb1slider and mylb2slider respectively. I noticed the callback is sometimes called twice (expand, collapse) or three times (mouse wheel) and does not update the listbox sliders correctly in the first run (seems to appear only within expand)…

    Does anyone know of this display bug/problem/whatever?

  3. Janos Marki says:

    Epic fail.

    I tried all three methods, none worked for me. I am using a GUIDE created function, and to start with, I have the problem that I have no idea where the listeners should be created (openingFCN, main function or outside of function?). However, if I would be past that, at the next step, I don’t understand what input parameters are given when a function is referenced simply as @myCbFcn. I have gotten as far as getting this function executed (when moving it out into a separate file as opposed to having it in the main GUI file), but then I just get crap from schema.prop and Java gibberish instead of getting the handles through these calls. Sorry, I just don’t understand…

    I’ll try to be more specific. In this example:

    hListener = handle.listener(hhSlider,hProp,'PropertyPostSet',@myCbFcn);

    What input arguments are passed to @myCbFcn??? I thought it would be hObject, EventData, handles, but it really is not…

    Thanks!
    Janos

    • Janos Marki says:

      Trying to break it down a little more. Trying to put it into fnname_OpeningFcn, nothing happens on the slider movement event:

      function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
       
      % Choose default command line output for untitled
      handles.output = hObject;
       
      % Update handles structure
      guidata(hObject, handles);
       
      set(handles.text1,'String',get(handles.slider1,'Value'))
       
      handle.listener(handles.slider1,'ActionEvent',@updatetextbox);
      %addlistener(handles.slider1,'ActionEvent',@updatetextbox); %using this variation does not work, either

      And updatetextbox looks like this in a separate file (i.e. once it registers a slide movement, execution should jump inside updatetextbox and give me keyboard access, so I can at least see what inputs it got – but it does not happen (probably because I defined the listener inside, at the OpeningFcn level?).

      function updatetextbox(varargin)
      keyboard
      slidval=get(handles.slider1,'Value');
      set(handles.text1,'String',num2str(slidval));

      If I define the listener in the main function, then there is a problem – it runs through a lot of times. I can catch one where the figure is plotted already (where varargout already exists), grab the handle, add the listener… and nothing happens (sigh).:

       
      function varargout = untitled(varargin)
      % UNTITLED M-file for untitled.fig
      %  *** standard comment section ***
      % See also: GUIDE, GUIDATA, GUIHANDLES
       
      % Edit the above text to modify the response to help untitled
      % Last Modified by GUIDE v2.5 11-Apr-2010 01:31:27
       
      % Begin initialization code - DO NOT EDIT
      gui_Singleton = 1;
      gui_State = struct('gui_Name',       mfilename, ...
                         'gui_Singleton',  gui_Singleton, ...
                         'gui_OpeningFcn', @untitled_OpeningFcn, ...
                         'gui_OutputFcn',  @untitled_OutputFcn, ...
                         'gui_LayoutFcn',  [] , ...
                         'gui_Callback',   []);
      if nargin && ischar(varargin{1})
          gui_State.gui_Callback = str2func(varargin{1});
      end
       
      if nargout
          [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
      else
          gui_mainfcn(gui_State, varargin{:});
      end
      % End initialization code - DO NOT EDIT
       
      % *** ADDED MY BIT HERE!!! ***
      if exist('varargout')==1       %stopping main function once figure is up
         handles=guidata(varargout{1});      %grabbing the handles structure
         handle.listener(handles.slider1,'ActionEvent',@updatetextbox);  %adding listener
      end

      In this case, I think as the whole namespace disappears once this instance of the function runs through, the listener disappears with it, too.
      (I’ll post this now, and continue in the next)

    • Janos Marki says:

      So in the post above, I tried implementing handle.listener at the Opening_Fcn and the main function level. Last option is outside the function, at the command prompt level. And this is what I do…

      >> ni=untitled
      ni =
        173.0035
       
      >> handles=guidata(ni)
      handles = 
          figure1: 173.0035
            text1: 1.0059
          slider1: 174.0031
           output: 173.0035
       
      >> handle.listener(handles.slider1,'ActionEvent',@updatetextbox);
       
      % *** Now try moving the slider ***
      % YES, IT WORKS, we get to updatetextbox...
      % So what arguments did updatetextbox get?
       
      K>> varargin
      varargin = 
          [1x1 uicontrol]    [1x1 handle.EventData]

      Right, great news. So to do this, now I need a callback function of this form:

      function updatetextbox(hObject, eventdata, handles)
      handles=guidata(hObject);
      slidval=get(handles.slider1,'Value');
      set(handles.text1,'String',num2str(slidval));

      And this finally works!!

      Phew. Now, on to the “interesting” question. How can I “package” this up into the GUI itself (which was, as said, created by GUIDE, so I can’t just put the listener line after the creation of a uicontrol object. Using a script, which launches the GUI, then contains the above commands in chronographical order?

      In fact, question no. 2:
      I chose GUIDE because the fact of not having to set uicontrol object positions and sizes by coding but graphically is very easy and fast for me. Was that silly, and is that the reason for all the above suffering? (i.e. once one needs something past very basic funtionality – e.g. continuous slider callback – GUIDE is NOT the way to go?

      In case there WAS anyone who followed through my “troubleshooting”, thanks, any further advice for a noob like me highly welcome.

      Cheers,
      Janos
      Last bit of info:
      I based this on the function untitled.m (and the associated saved untitled.fig) from here:
      http://www.mathworks.com/support/solutions/en/data/1-3SR0YI/index.html?product=ML&solution=1-3SR0YI

      Which is supposed to be a solution for exactly this problem. On its own, it did not work, and that is when I tried implementing your solutions (though it is supposed to be what I am looking for, i.e. continuous slider callback from within a GUIDE-created GUI)

    • @Janos – hListener=handle.listener requires you to store the returned hListener handle somewhere – the listener only works as long as its handle is stored somewhere. A good place to store this listener is in the target object’s ApplicationData, using the built-in setappdata function:

      hListener=handle.listener(handles.slider1,'ActionEvent',@updatetextbox);
      setappdata(handles.slider1,'myListener',hListener);

      You can do this in your main code – no need to use the Command Window. The reason it worked for you in the Command Window is that the handle.listener result was kept in the ans variable, so as long as ans was not changed the listener worked.

    • Janos Marki says:

      @Yair:
      I see, makes sense! I put your 2 lines of code into my function’s OpeningFcn, the Callback function back at the bottom of the main GUI .m-file and it all works now, thanks! Sorry for polluting the thread with such a looooong entry! :-)

    • Glad to help – that’s what I’m here for…

      The most difficult questions sometimes have very simple solutions, as in this case

    • Janos Marki says:

      Thanks again, I even ended up using the “Donate” button. I don’t do that often, so you can be sure of seeing me here again!

    • Much obliged, Janos :-)

      Positive feedback such as yours is very important for me when I have to explain to my family why I spend so much time on this website, for no apparent benefit…

  4. Cemil Kirbas says:

    Hi,

    I created a GUI using guide with two sliders to adjust image window and level. When I use setappdata to my slider object a new figure is created when I started to use the slider. My image is displayed in the new figure instead of my current GUI figure. Any idea why this is happening. Your help is appreciated. Thanks.

    Cemil

    • @Cemil – this could be due to several reasons: My bet is that your figure has a hidden handle (HandleVisibility=’off’) and in your callback function you are using the gcf function or some other similar function that creates a new figure in such cases. Instead, use the actual figure handle that is available in your handles struct and ensure that you pass this handle to every function that could possibly need it (axes etc.).

  5. Cemil Kirbas says:

    Thank you very much Yair. That was a great help. I changed HandleVisibility of the figure from its default value ‘callback’ to ‘on’ and the problem is solved. I appreciate your help.

    Best regards,

    Cemil.

  6. Florin says:

    Hi,

    I have been reading this post, but I still have a problem. I am reading and a .tif file and display slices of it with imagesc. I am trying to continuously update this images when moving the slider. I have the same problem as Cemil : a new windows appears and the imagescs are updated in that new figure. As soon as I release the mouse button the intended figure inside my GUI is also updated. I checked the HandleVisibility parameter. I also tried to set the handle with

    figure(get(handles.axes1,'Parent'));

    Any suggestions for me, pls ?

    Thank you.
    Florin

    • @Florin – you probably have the same problem as Cemil and the solution is similar. In your slider callback, instead of using a simple imagesc, directly specify the parent axes:

      imagesc('Parent',handles.axes1, 'XData',xdata, 'YData',ydata, ...);
    • Florin says:

      Yair,

      Thank you for your quick reply. That worked.
      Hope you can maintain this site for a long time.
      I wish you all the best.

      Regards,
      Florin

  7. Jonas says:

    Dear Yair
    Your blog is just wonderful and helps me a lot these days in getting somewhere with my thesis. Thank your for your good work!

    Regarding the continuous slider callback, I wonder what the difference between

    hListener = handle.listener(handles.mySlider,'ActionEvent',@mySliderMoved);
    setappdata(handles.mySlider,'mySliderListener',hListener);

    and

    hListener = addlistener(handles.mySlider, 'ActionEvent', @mySliderMoved);

    exactly is…?

    Best,
    Jonas

    PS. looking forward to your relevations of 2011a – coming tomorrow :)

    • @Jonas – these are essentially the same, except that addlistener adds the listener in a hidden property called Listeners__, rather than in the similarly-hidden ApplicationData property (which is what setappdata does).

      >> get(handles.mySlider, 'Listeners__')
      ans =
      	handle.listener

      The actual place where the listener is stored does not matter. It is only important to store listeners together with the listened object, so that they both have exactly the same life-span. Both of these methods do that, by storing the listener in one of the object handle’s properties.

      The source code for addlistener is not available in the latest Matlab releases (it’s implemented as an internal function), but it is available and inspectable in Matlab releases up to 2008 (e.g., C:\Program Files\Matlab\R2008a\toolbox\matlab\uitools\private\addlistener.m). This source code is not long (only ~30 code lines) and it’s interesting, so I recommend looking at it.

  8. Jonas says:

    A short update form my side – as I now run on R2011a.

    I used to run a listener as proposed (R2010a/b) in the post the following way:

    lh = addlistener(sliderhandle, 'ActionEvent',@MyCallback);

    The new Version of Matlab (R2011a) gives me a warning that I should not use this method anymore but rather use: a “ContinuousValueChange” Event.
    So I end up with:

    lh = addlistener(sliderhandle, 'ContinuousValueChange',@MyCallback);

    which works pretty smooth. However, besides of that one-time-console-warning I have not found any documented information about this “ContinuousValueChange”-event or other hidden events…

    Best, Jonas

  9. Pingback: Matlab continuous slider callback « Desperate Engineers Blog

  10. Elise says:

    Thanks! Handle.listener worked great!

  11. claus says:

    Hi, thank you for your help!

    I turned my sliders into continuous just by adding two simple mods:

    first, add the following 2 lines to your xxxxxx_OpeningFcn:

    hListener=addlistener(handles.sliderN, 'ContinuousValueChange', @sliderN_Callback);
    setappdata(handles.sliderN,'myListener',hListener);

    then, add the following 3 lines to your sliderN_Callback:

    if ~(exist('handles','var'))
         handles=guidata(hObject);
    end

    It works like a charm!

    Thank you!

  12. John says:

    Hi guys,

    i just wanted to add my solution to the continuous slider problem, which seems to be the simpliest/easiest way to understand (imho):

    1. Create the control:

        myslider = uicontrol(topWindowHandle, ...
            'Style', 'slider', ...
            'Min', 0, 'Max', 100, ...
            'Value', 25, ...
            'SliderStep', [0.05 0.2], ...
            'Position', [60 100 350 18]);

    2. Add a listener to the Value-property:

    addlistener(handle(myslider), 'Value', 'PostSet', @slider_callback);

    3. Implement the slider-callback-function:

        function slider_callback(hObject, eventdata)
            obj = get(eventdata, 'AffectedObject');
            val = get(obj, 'Value');
            display(val);
        end

    Now, val contains the actual slider value, because the AffectedObject-property holds the desired slider control, from which we can obtain our slider-value.

    Greetings

    John

  13. Pingback: Waterloo graphics | Undocumented Matlab

  14. Pingback: Customizing figure toolbar background | Undocumented Matlab

  15. Jing says:

    Hi, Thank you for posting this nice solution. But I have some troubles using it in GUIDE.

    Everything works out fine except that the callback function on mySlider is always called twice!
    The first time, hObject passed from callback function is the slider itself; The second time the hObject = uicontrol.

    I’m trying to pass handles in this callback function, but in the case when hObject = uicontrol, the data in handles never got updated. This is the real problem that troubles me.

    Please help, Thank you.

  16. DSN says:

    Thanks a lot Yair :) . clean and neat solutions.

  17. Aya says:

    Yair,

    What exactly is ‘hScrollpanel’ in your “Using an event listener” option? I am trying to replicate your code, but I do not know where you got this ‘hScrollpanel’ from..thanks.

    • @Aya – hScrollpanel is simply part of Matlab’s imscrollpanel code that I presented to show how the concept is being used. It is merely a handle to a panel. You can use any Matlab control to store the listener handle, including hSlider itself, which would be simplest:

      setappdata(hSlider,'sliderListener',hListener)

      It doesn’t matter where or how exactly you store hListener, since you will not use it directly in your program. The important thing is just to store it *anywhere*, so that it remains in persistent (heap) memory. As long as the reference handle is “alive”, the listener will keep working. This was explained here.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

<pre lang="matlab">
a = magic(3);
sum(a)
</pre>