Add listener in slider CreateFcn

2 views (last 30 days)
Hi. I am trying to add listner in slider CreateFcn, but I am getting error "Unbalanced or unexpected parenthesis or bracket.". I am doing this in GUIDE.
function slider1_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
addlistener(hObject, 'ContinuousValueChange', @slider1_Callback(hObject, eventdata, handles))

Accepted Answer

Walter Roberson
Walter Roberson on 25 Nov 2015
addlistener(hObject, 'ContinuousValueChange', @(hObject, eventdata) slider1_Callback(hObject, eventdata, handles))
But be careful: it is the value of handles as of the time of the call to addlistener that will be "captured" and passed in to the slider1_Callback. Any changes you might make to handles after that call will not be reflected.
For this reason I recommend passing the figure number of the GUI instead, and then in the callback routine, using guidata() on the GUI figure to pull out the current version of the handles structure.
Exception: if you are using only a single figure (including the GUI) then I would not bother to pass handles and instead in the callback would use
handles = guidata(hObject);
to pull out the current handles structure. You can do this as long as the callback object is part of the GUI figure; if the callback object is part of some other figure then you need to know the GUI figure number (or at least the tag of something known to be part of the GUI figure.)
  2 Comments
Dmitry Devjatykh
Dmitry Devjatykh on 26 Nov 2015
Edited: Walter Roberson on 26 Nov 2015
Error:
Attempt to reference field of non-structure array.
Error in myGui>slider1_Callback (line 108)
set(handles.text1,'String',num2str(sliderVal));
Error in myGui>@(hObject,eventdata)slider1_Callback(hObject,eventdata,handles) (line 121)
addlistener(hObject, 'ContinuousValueChange', @(hObject, eventdata) slider1_Callback(hObject, eventdata,
handles))
Warning: Error occurred while evaluating listener callback.
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', @(hObject, eventdata)
slider1_Callback(hObject, eventdata, handles))
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
sliderVal = get(hObject, 'Value');
set(handles.text1,'String',num2str(sliderVal));
Walter Roberson
Walter Roberson on 26 Nov 2015
Look at the comment in your code:
% handles empty - handles not created until after all CreateFcns called
Please re-read what I wrote starting from "For this reason I recommend"

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!