Continuous slider callback, how to get Value from addlistener?

52 次查看(过去 30 天)
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);

采纳的回答

Stephen23
Stephen23 2016-1-24
编辑:Stephen23 2019-2-21
A typical usage would be like this:
addlistener(Handle, 'Value', 'PostSet',@MyCallBack);
And note that while MyCallBack is a function, it does not have any output arguments. However when that function is called you can access the slider value very easily within that function.
Here is a minimal example that simply displays the new value:
MyCallBack = @(a,b) disp(get(b,'NewValue'));
F = figure();
H = uicontrol(F,'Style','slider');
addlistener(H, 'Value', 'PostSet',MyCallBack);
Again note that the new value is available inside the MyCallBack function workspace, so it is within this callback function that you need to use the new value as a variable. The easiest way is to write a new function just for the callback.
Tip
It would improve your code if you used guidata instead of using assignin:
  5 个评论
zhikai
zhikai 2017-2-22
I think Matlab updated this function recently. For this part:
get(event,'newValue')
Now, maybe you should try the following:
get(event.AffectedObject,'Value')
It worked on my R2016b version.
Michiel Hermes
Michiel Hermes 2019-1-25
编辑:Michiel Hermes 2019-1-25
Zhikai is correct in the 2018 version you seem to need:
CallBack = @(~,b) disp(b.AffectedObject.Value);
F = figure();
H = uicontrol(F,'Style','slider');
addlistener(H, 'Value', 'PostSet',CallBack);

请先登录,再进行评论。

更多回答(1 个)

AG
AG 2019-2-20
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 个评论
Stephen23
Stephen23 2019-2-20
编辑:Stephen23 2019-2-21
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by