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

8 views (last 30 days)
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)

Jan
Jan on 20 Jun 2017
Edited: Jan on 22 Jun 2017
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
Zhengyi
Zhengyi on 16 Jan 2018
Edited: Zhengyi on 16 Jan 2018
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.


Stephen23
Stephen23 on 22 Jun 2017
Edited: Stephen23 on 22 Jun 2017
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
Stephen23
Stephen23 on 22 Jun 2017
Edited: Stephen23 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
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.

Sign in to comment.

Categories

Find more on Programming 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!