Continuous slider callback, how to get Value from addlistener?

조회 수: 16 (최근 30일)
Mohamad Roslan Mohd Roshdi
Mohamad Roslan Mohd Roshdi 2016년 1월 24일
편집: Stephen23 2019년 2월 21일
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에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by