How do I set the string of an edit text using a slider and vise versa?

22 views (last 30 days)
My current code for the edit text is:
set(handles.edit1, 'String', num2str(get(handles.slider1, 'Value')));
What I need the slider to do is display the value of the slider and also have the edit text be able to set the slider.

Answers (2)

Geoff Hayes
Geoff Hayes on 8 Aug 2014
Giann - in order to display the slider value in the edit text widget, you will need a callback. For sliders, a continuous value change listener can be used to handle this callback. Assuming that you are using GUIDE, in the opening function of your GUI, do the following
function yourGuiName_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for untitled
handles.output = hObject;
% create the listener for the slider
handles.sliderListener = addlistener(handles.slider1,'ContinuousValueChange', ...
@(hObject, event) slider1ContValCallback(...
hObject, eventdata, handles));
% default the edit text to the min slider value
set(handles.edit1, 'String', num2str(get(handles.slider1,'Min')));
set(handles.edit1, 'String', num2str(0));
% update handles structure
guidata(hObject, handles);
All we are doing is setting up the continuous value change callback for the slider, and setting the listener object as a field value within the handles structure. guidata then saves the updated handles structure. We also initialize the edit text value to be the minimum slider value.
Now we define the slider1ContValCallback callback using your code from above
function slider1ContValCallback(hObject, eventdata, handles)
set(handles.edit1, 'String', num2str(get(handles.slider1, 'Value')));
That part is easy. More challenging is going in the other direction. Look at the KeyPressFcn callback for the edit text widget, and start from there.
  6 Comments
Kiran Reddy
Kiran Reddy on 5 Nov 2015
</matlabcentral/answers/uploaded_files/39681/mathworks.jpg> sorry for not being clear about my question see the image this my gui slider is one which updates the value in the text . this the code
function edit1_Callback(hObject, eventdata, handles)
set(handles.slider1, 'Value', str2num(get(handles.edit1, 'String')))
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
set(handles.edit1, 'String', get(hObject,'Value'));
i need updated information from either of the callbacks to transfer to some other function where i can do desired analysis(like Run result callback) ...for example as show in figure i need the update value in figure temperature edit text (the value can be directly enter into edit box which initates edit callback or by moving slider which initiated slider call back) ,,,i used setappdata function to transfer the data to other function this means i need use to two setappdata commands(diferent handles) which are acquired by getappadata in other function if do so i will create two duplicate values which is not desirable ..
finally what am trying to say ....i need to transfer the updated value in the edit text to someother function (updated value can done either directly entering the value or by using slider ....if use get function to get from edit box then it will transfer only data which is directly entire in the edit box by edit callback and not the value by using slider ...if i use get function in slider it will not transfer the data which is directly entered in the box.)

Sign in to comment.


Joakim Magnusson
Joakim Magnusson on 11 Aug 2014
If you want the edit box to update while the slider is being moved you should do as Geoff said. The solution is more simple if it's enough that the edit box updates when the slider is released. Then you could just do the following:
In the callback for the edit box, just write: set(handles.slider, 'Value', str2num(get(handles.editTextBox, 'String')));
In the sliders callback write: set(handles.editTextBox, 'String', get(hObject,'Value'));

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!