How to update an edit box window continuously by dragging a slider in Matlab GUI?

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

Thank you for your answer Mr. Simon.
Here is a piece of code.
We created a separate Slider to control a robot movement in an external application.
We also created addlistener to get the values continuously with a slider.
Please tell us how i can get the values from addlistener slider to edit box
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global val
H=uicontrol(Slider_test,'Style','slider');
addlistener(H,'Value','PostSet',@myCallBack);
set(handles.edit1,'String',num2str(val));
function myCallBack(hObj,event)
global val
val=get(event.AffectedObject,'Value')
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.
Add the listener once in the OpeningFcn only.
However, if you use the code I show, you do not need a listener. The slider Callback is acting as a listener already.
@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.
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))
I've moved the slider for 20 seconds without a callback. The documentation says:
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);

Sign in to comment.

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

Thank you Mr. Cobeldick
Can you please explain your code based on this context for which we need to get the value inside the edit box (Photo 2).
We could not understand the code especially @(o,e) fun = @(o,e)set(ht,'String',num2str(get(e,'NewValue')));
We tried your code and got an error(attached photo 1). Could you please explain the same.?
"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')));
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.

Sign in to comment.

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!