How do I update my slider using an edit text?

22 views (last 30 days)
So my edit text is able to change the value of my slider, but only after the slider is clicked on, is there any way that the slider could be updated continuously or after I hit enter? Here is my code in the slider callback:
val=str2num (get (handles.edit1, 'String'));
set(handles.slider1, 'Value', val);
  1 Comment
Paul
Paul on 8 Aug 2014
Edited: Paul on 8 Aug 2014
I would suggest that you create a generic callback function, and give each of your objects a Tag with an appropriate name.
In this case, the edit box would have
set(handles.edit1,'Tag','handles.edit')
and slider would be
set(handles.slider1,'Tag','handles.slider1')
Assign a generic callback to each
set(handles.edit1,'Callback',@genericCB)
set(handles.slider1,'Callback',@genericCB)
Now you create the generic callback that reads the Tag
function genericCB(src,eventdata)
handleLink = get(src,'Tag')
if any(regexp(handleLink,'edit1'))
% The edit1 object was called, update slider1
elseif any(regexp(handleLink,'slider1'))
% The slider1 object was called, update edit1
end
Edit: When you enter a value into the edit box, the callback will not be called until you click outside of the edit box or press on the keyboard. If you want the slider to update as you are typing, I believe you'll need to create a java object instead of a uicontrol object

Sign in to comment.

Accepted Answer

Evan
Evan on 8 Aug 2014
Edited: Evan on 8 Aug 2014
To make the slider update when the editbox is updated, put the update code in the editbox callback
function myEditBox_Callback(hObject,eventdata,handles)
val = str2double(get(hObject,'String'));
set(handles.slider1,'Value',val);
guidata(hObject,handles)
And then, in the slider's callback:
function slider1_Callback(hObject,eventdata,handles)
val = get(hObject,'Value');
set(handles.myEditBox,'String',num2str(val));
guidata(hObject,handles)
  1 Comment
Jo
Jo on 9 Jun 2021
Can you show me the full code please?

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps 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!

Translated by