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.
Addedndum 2014-08-20: In R2014a the event name has changed from ActionEvent to ContinuousValueChange. Also, handle.listener will not work in the upcoming HG2. Therefore, it would be best to use the following code snippet instead:
try % R2013b and older addlistener(hSlider,'ActionEvent',@myCallbackFcn); catch % R2014a and newer addlistener(hSlider,'ContinuousValueChange',@myCallbackFcn); end |
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'); try % R2014b and newer % hProp is a matlab.graphics.internal.GraphicsMetaProperty object addlistener(hSlider,hProp,'PostSet',@myCbFcn); catch % R2014a and older % hProp is a schema.prop object hListener = handle.listener(hhSlider,hProp,'PropertyPostSet',@myCbFcn); setappdata(hSlider,'sliderListener',hListener); % this is important - read above end |
In addition to ‘PostSet’, we could also listen on ‘PreSet’, which is triggered immediately before the property is modified. There are also corresponding ‘*Get’ options.
Do you know of any other way to achieve continuous callbacks? If so, I would be delighted to hear in the comments section below.
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:
For the screensize issue try:
Not sure if there is some kind of listener for changes in that
The screensize can also be gotten via pure Matlab:
Note that if you have multiple concurrent monitors this gets a bit trickier.
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.
@Yair – unfortunately
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
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.
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.
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?
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:
What input arguments are passed to @myCbFcn??? I thought it would be hObject, EventData, handles, but it really is not…
Thanks!
Janos
Trying to break it down a little more. Trying to put it into fnname_OpeningFcn, nothing happens on the slider movement event:
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?).
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).:
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)
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…
Right, great news. So to do this, now I need a callback function of this form:
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:
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.
@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
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…
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.).
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.
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
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:
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
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
and
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).
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.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:
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:
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
[…] ([uicontrol('Style','slider', …]) in a MATLAB GUI. Thanks to the posting of Yair Altman on this topic (on his awesome Undocumented Matlab Blog which you shoudl definitely check out), I had no problem […]
Thanks! Handle.listener worked great!
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:
then, add the following 3 lines to your sliderN_Callback:
It works like a charm!
Thank you!
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:
2. Add a listener to the Value-property:
3. Implement the slider-callback-function:
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
This is awesome!
Thank you!
[…] One of Matlab’s limitations is the quality of its 2D graphics. Plots are more pixellated and less interactive than some might expect in a modern programming environment. […]
[…] For the slider I used a javax.swing.JSlider having a continuous-movement callback […]
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.
@Jing – you could add the following at the top of your calback function:
@Yair – Thank you so much. Sorry for my late late reply…
Thanks a lot Yair :). clean and neat solutions.
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, includinghSlider
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 was explained here.Hi,
From your blog, I learned a lot. What you are talking about in this blog is event listener within a gui . however, if a listener is set to slider in the main gui, update text in the child gui in the response function of the listener , how to do it? I’m troubled.
In the response function ,obtaining handle of the child: handles = guihandles (sub); then use the set function, but doesn’t work, why? Moreover, the slider in the main gui has listener response function and callback function , when dragging with the mouse, how these two functions work together?
Please help me ,thanks.
@Young – I suggest that you read and experiment with Matt Fig’s GUI examples to understand how Matlab GUI callbacks work.
What are the arguments passed into the ‘AdjustmentValueChangedCallback’ function? I saw them mentioned somewhere, here on this site or in your book, but I can’t seem to find them again.
Thanks so much,
Bill
@Bill – it’s exactly like any other Matlab callback function. It accepts an object reference as first input arg, followed by an
eventData
object (that includes information on the triggered event). You can also specify additional extra parameters. See the official docpage in the previous link for details. Also read my description of Matlab callbacks for Java control events.@Yair Thanks … I noticed some quirky behavior and I don’t know if it’s something I forgot to do or just what is going on.
Using the following simple test:
abc.m:
def.m
I get different values for the slider depending on how I try to get it:
I also noticed that the AdjustmentValueChangedCallback was called multiple times when I clicked inside the slider or moved the slider button.
Am I missing something?
Again thanks a bunch!!
Bill
@Bill – Java sliders have different model values for the slider than Matlab. Since you have my book, look at page 418. You simply need to convert between the increment and min/max values using a simple linear transformation formula. But I really don’t understand why you’d want to do this if you can simply listen to the event at the Matlab level using the addlistener technique that I explain in this article.
@Yair … I’ll try that approach.
Thanks again.
Bill
I am new to matlab and I need your help please.
I have several subplots in a collum then I create an horizontal slider to see better some parts of the subplots data. meanwhile I create a patch to be able to select a region (selecting band) of the subplots and extract the data. My problem is that I want to drag the selecting band(as I drag the mouse it increase or decrease de selection area) and by doing that I need that the slider adjust its position, even more important I need to be able to adjust the plot horizontally as the selection region increase to the right (like if I press the slider button) or to the left.
Basically when I increase the selection band to the right or to the left I need that plot behaves like I was pressing the slider buttons to the right or to the left.
Iam lost about how to perform this can anyone help showing me some possible code to achieve this?
@Amindo – perhaps ScrollPlot on the File Exchange will help you. If not, then contact me offline (by email) for a a consulting proposal.
Well, for some reason TMW has elliminated the ‘ContinuousValueChange’ event for the sliders in R2015a, an it will not work in this version…. Even worse, there is no comparable event that one could think of using.
That’s a pity, I had many code based in this approach, which is know perfectly useless :-/
@Daniel – the
ContinuousValueChange
event still exists. It is simply hidden, but should be fully functional:Other hidden events of sliders include:
Action, KeyPress, KeyRelease, LocationChanged, SizeChanged, ButtonDown, Reset
Additional information: https://undocumentedmatlab.com/blog/undocumented-hg2-graphics-events
Yes! Fewh, my code will still works with some changes.
Actually, my code was written defensively, checking explicitly that ‘ContinuousValueChange’ is in the list of events in a handle… the result is that the code refuses to create the listener to an apparently non available-event.
Thanks for pointing me to your list of events in HG2: it is the only source of information I can use to harness events!
… I really wonder why TMW has hidden these events: I don’t see any advantage, and it makes life much harder…
@Daniel – this is one of the reasons that I use try/catch rather than if/else
Thank you for the help.
I think that I can solve the problem if I can converte the x coordenate obtained thru the get(gca’CurrentPoint’) to the slider scale. There is a easy way to do this?
kind regards
Matlab R2015a:
Cordially.
Thanks Seif – I clarified the post text accordingly.
Hello,
I have some performance issues when using the addlistener in addition to WindowButtonMotionFcn:
Is there a way to suppress the WindowButtonMotionFcn of the figure during the continuous slider callback? I tried using the WindowButtonDownFcn/WindowButtonUpFcn to remove/add the WindowButtonMotionFcn, but WindowButtonDownFcn nor ButtonDownFcn is called when the slider property ‘Enable’ is set ‘on’. Using ‘inactive’ first would a require a second click to tell Matlab to drag the slider…
Thanks, Felix
May add a listener to ButtonDownFcn/ButtonUpFcn ? I tried various events but only got errors like: