Info

This question is closed. Reopen it to edit or answer.

Addlistener for slider in GUIDE for constant update

2 views (last 30 days)
Hello. I used code from this topic http://www.mathworks.com/matlabcentral/answers/56236-how-to-constantly-update-a-plot-off-of-a-slider-being-pulled it works great with programmatialy created UI.
function myslider
x = 1:10;
hplot = plot(x,0*x);
h = uicontrol('style','slider','units','pixel','position',[20 20 300 20]);
addlistener(h,'ActionEvent',@(hObject, event) makeplot(hObject, event,x,hplot));
function makeplot(hObject,event,x,hplot)
n = get(hObject,'Value');
set(hplot,'ydata',x.^n);
drawnow;
So the question is: how to addlistener for slider in GUIDE? Do i have to do it in CreateFcn? Overall idea is to constantly update plot with slider pulled, not just clicked, but I want to do it in guide

Accepted Answer

Adam
Adam on 24 Nov 2015
I usually add listeners and other things done to ui components in the OpeningFcn (or rather, a function called from that usually), but I guess you could do it in the createFcn of the particular component. I never use the createFcn personally, but it will get called at a time suitable for what you want to do.
  2 Comments
Dmitry Devjatykh
Dmitry Devjatykh on 25 Nov 2015
Edited: Dmitry Devjatykh on 25 Nov 2015
I added listener in CreateFcn
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
addlistener(hObject, 'ContinuousValueChange', @slider1_Callback);
And here is CallBackFcn
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (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
plot(handles.axes4,sin(get(hObject,'Value')))
When I start moving slider I get error
Walter Roberson
Walter Roberson on 25 Nov 2015
You went and created a duplicate question and I answered there before I noticed. Now you have two questions on the same topic to confuse you and me and Adam and everyone else who comes along trying to find solutions to anything similar :( :(

More Answers (0)

Community Treasure Hunt

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

Start Hunting!