How to update an edit box window continuously by dragging a slider in Matlab GUI?
Show older comments
I want to control a each joint of robot using Matlab GUI. When I wish to move the slider the joint is moving but the movement indicated by the number (in edit box) is not getting updated continuously when the slider is moved.
P.S : addlistener function is not working since implicit calling of function is not possible for our case
Answers (2)
I'm not convinced that the addlistener is not working. Please explain your opinion about "implicit calling of functions", because this is actually teh best and most direct solution. Any callback method will be a kind of implicit calling, so there is no other way.
If a slider is really not working for you, you can simulate a slider by drawing some graphic objects manually and track mouse clicking and moving using the WindowButtonDown and WindowBuittonMotion function. But hand made GUI elements are a mess and using already existing methods is much smarter.
I assume the solution is to get rid of the problems you have with "implicit calling of functions" and use the standard listener method as explained at Answers: continuous-slider-callback .
[EDITED] This thread already contains all hints, which are needed. You are still posting the same piece of code. So let me try to summarize the steps to solve the problem:
- Add the listener in the OpeningFcn:
addlistener(handles.Slider1 ,'Value', 'PostSet', @ContSliderDragCB);
- Now the callbacks for the slider and the listener:
function slider1_Callback(hObject, eventdata, handles)
Value = get(hObject, 'Value');
set(handles.edit1,'String', num2str(Value));
function ContSliderDragCB(hObject, eventdata)
handles = guidata(hObject);
Value = get(eventdata.AffectedObject, 'Value');
set(handles.edit1,'String', num2str(Value));
- Avoid globals, because they are a shot in your knee. There is not need for a global, because the value of the slider is stored in the slider already.
10 Comments
Thiruvikraman Sourirajalu
on 21 Jun 2017
Walter Roberson
on 21 Jun 2017
You should not be creating a new uicontrol each time the slider moves. You do not need the listener. You should create the slider ahead of time (which you did in GUIDE) and then the slider1_Callback should just be
global val
val = get(hObject, 'Value');
set(handles.edit1, 'String', num2str(val))
Which would be even better if you got rid of the global.
Jan
on 21 Jun 2017
Add the listener once in the OpeningFcn only.
Walter Roberson
on 21 Jun 2017
However, if you use the code I show, you do not need a listener. The slider Callback is acting as a listener already.
Jan
on 21 Jun 2017
@Walter: The slider callback is triggered, when the mouse is released, while the listener callback reacts to moving the slider already, which allows a direct visual feedback.
Walter Roberson
on 21 Jun 2017
Sliders callbacks do fire while the slider is being dragged, but it is not clear how often.
@Walter: At least in R2016b and earlier versions the callback is fired only, which the mouse is released, when I drag the slider:
figure
uicontrol('Style', 'Slider', 'Callback', @(h,e) disp(h.Value))
The location of the thumb indicates a numeric value, assigned to
the Value property when you release the mouse button.
It is something else when I click and hold on the arrows or in the slider bar. Then the callback is fired with each change of the value.
In my tests now, the callback gets triggered while clicking the end arrows, but never triggered while dragging the bar:
uicontrol('style','slider','callback',@(o,e)disp(get(o,'Value')));
Whereas the listener is triggered "continuously" both while dragging the bar and also while clicking the end arrows:
h = uicontrol('style','slider');
addlistener(h, 'Value', 'PostSet',@(o,e)disp(get(e,'NewValue')));
The OP's title states "..update an edit box window continuously by dragging a slider..", which would imply to me that a listener is required.
In R2017a, the first input variable in 'postset' or 'preset' event callback is no longer the figure object but GraphicsMetaProperty. Therefore,
handles = guidata(hObject);
would throw a warning.
Is there a new way to get guidata?
Edit: Shortly after I post the question, I found this would work...
handles = guidata(eventdata.AffectedObject);
Vinayak Appasaheb Bhatte
on 9 Jul 2018
@ walter Thanks it worked :) short and sweet
Here is a complete working solution for pre-R2014b MATLAB, that updates an edit box while dragging the slider:
ht = uicontrol('style','edit','Position',[10,60,40,40]);
hs = uicontrol('style','slider','Position',[10,10,400,20]);
fun = @(o,e)set(ht,'String',num2str(get(e,'NewValue')));
addlistener(hs, 'Value', 'PostSet',fun);
And it looks like this:

3 Comments
Thiruvikraman Sourirajalu
on 22 Jun 2017
"We tried your code ..."
No, you didn't. Why are you adding a listener inside a callback function? What is the point of defining a new slider inside a callback function? Why write code using awful globals, when globals are not required at all?
The code I posted does not do any of these things. The code I posted works (pre 2014b), which I know because I tested it (and made a screenshot for you to see). I do not use GUIDE because I prefer to write my own code, so I cannot help you with GUIDE, if that is what you are using. What I showed you is, as my answer clearly says, a "complete working solution": this means it works just by running those four lines of code.
"We could not understand the code especially fun =..."
fun is an anonymous function that gets the slider numeric value, converts it to string, and then sets that string as the edit-box string value. fun is called by the listener, and so is called every time the slider changes, and thus updates the edit string.
For post R2014b something like this might work (untested):
fun = @(~,e)set(ht,'String',num2str(get(e.AffectedObject,'Value')));
THIRUVIKRAMAN SOURIRAJALU
on 10 Jul 2018
Hello Mr. Cobeldick.
I apologise for acknowledging your ideas very late.
For creating the slider alone, your code is absolutely flawless and we can use it. Our idea is to control a robot model in Virtual Reality(VRML). So we create GUI elements (Eg: Sliders, Edit Text Box, Radio Buttons) and interface the callback functions to a prewritten code. That is the reason why we declared global varibles and our Working Platform is GUIDE to create a simple GUI interface to select some frames of reference and to drag the slider thereby simulating the robot movements for better understanding and visualisation.
I hope that you can suggest some ideas based on this information.
Thanks in advance.
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

