Continuous slider callback, how to get Value from addlistener?
Show older comments
Hi I use GUIDE to create slider I tried to get value from continuous slider call back using addlistener I follow instruction from http://www.mathworks.com/matlabcentral/answers/51668-getting-gui-slider-updates-while-dragging
which utilize
h = uicontrol('style','slider','callback',@(src,evt)disp(get(src,'value')));
addlistener(h,'Value','PreSet',@(~,~)disp('hi'));
and I adjust the code accordingly, and it work as it should.

But it seem I cant get to contain the displayed value into variable, How to put the display value into variable?
here is my code.
% --- Executes on slider movement.
function kvslider_Callback(hObject, eventdata, handles)
% hObject handle to kvslider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
kV= get(hObject,'Value');
kv_value =(((round(kV/5))*5))
assignin('base','kV',kV);
assignin('base','kVp',kv_value);
set(handles.kvvalue,'String',strcat((num2str(kv_value)),' kVp'));
set(handles.kvslider,'Value',(kv_value));
setappdata(handles.kvslider, 'Value', num2str(kv_value));
%slidervalue = (round(slidervalue/3))*3
%experimental
kv_update = handles.kvslider;
kv_up =addlistener(kv_update,'Value','PreSet',@(~,~)disp(((round((get(kv_update , 'Value'))/5))*5)));
assignin('base','kv_up',kv_up);
%addlistener(kv_update,'Value','PreSet',@Apply_Callback);
Accepted Answer
More Answers (1)
AG
on 20 Feb 2019
The following worked for me. I get a 'live' scroll-bar update by calling this within another function:
slider_value = get(gcf.Children(j), 'Value');
...where j is the value of the UIcontrol corresponding to the scroll. You can find out which UIControl it is in the figure by putting a break in the code and using:
gcf.Children
This could also be ascertained in run-time using:
for j = 1:length(gcf.Children)
get(gcf.Children(j), 'Tag')
end
which will return a char array with the Tag of each of the children which could then be compared using strcmp(), for example. I've done this using a GUI created with GUIDE.
1 Comment
To get continuous updates with the slider movement (which the original question requested) requires adding a listener to that slider object. Your answer does not use a listener, and does not provide continuous updates based on the slider movement. All you are doing is polling the slider value by "another function".
Note that obtaining and using explicit handles for all graphics objects is ultimately simpler and much more reliable that than using gcf and the very indirect approach that you are using.
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!