Effacer les filtres
Effacer les filtres

Continuous slider callback, how to get Value from addlistener?

52 vues (au cours des 30 derniers jours)
Mohamad Roslan Mohd Roshdi
Modifié(e) : Stephen23 le 21 Fév 2019
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);

Réponse acceptée

Stephen23
Stephen23 le 24 Jan 2016
Modifié(e) : Stephen23 le 21 Fév 2019
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 commentaires
zhikai
zhikai le 22 Fév 2017
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 le 25 Jan 2019
Modifié(e) : Michiel Hermes le 25 Jan 2019
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);

Connectez-vous pour commenter.

Plus de réponses (1)

AG
AG le 20 Fév 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 commentaire
Stephen23
Stephen23 le 20 Fév 2019
Modifié(e) : Stephen23 le 21 Fév 2019
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by