??? Undefined variable "handles" or class "handles.slider1". in Guide for a function

9 views (last 30 days)
Eugene
Eugene on 1 Dec 2014
Commented: Eugene on 2 Dec 2014
I have 3 sliders. When I move one of the sliders I want to get the summ of Sliders Values. But summing of the Sliders Value I want to realise in separate function.
function slider1_Callback(hObject, eventdata, handles)
result;
function slider2_Callback(hObject, eventdata, handles)
result;
function slider3_Callback(hObject, eventdata, handles)
result;
function result
x=get(handles.slider1,'Value'); %position of X slider
set(handles.text1,'String', num2str(x));
y=get(handles.slider2,'Value'); %position of Y slider
set(handles.text2,'String', num2str(y));
z=get(handles.slider3,'Value'); %position of Z slider
set(handles.text3,'String', num2str(z));
result=x+y+z;
set(handles.textResult,'String', num2str(result));
If I moves one of the slider (the 3rd for example):
??? Undefined variable "handles" or class
"handles.slider1".
Error in ==> subfunction>result at 167
x=get(handles.slider1,'Value');
%position of X slider
Error in ==> subfunction>slider3_Callback at 151
result;
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> subfunction at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)subfunction('slider3_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
I try to add (hObject, eventdata, handles) to result function, but it doesn`t help. I`m not realy good in GUIDE functions, can someone help me, please?

Accepted Answer

Andrew Reibold
Andrew Reibold on 1 Dec 2014
Edited: Andrew Reibold on 1 Dec 2014
Notice in all the other functions you pass in handles which allows you to access the data inside
slider1_Callback(hObject, eventdata, handles)
In your return function. You are not passing handles into it, so it doesn't know what handles is. In fact, you are not passing anything into it or out of it.
function result
Imagine your functions have no idea what is available even in the workspace before calling them unless you tell them/give it to them. You will need to pass handles into the function so that it can access it.
maybe even something simple like below would help on the path of fixing.
function result(handles)
Edit: Somehow I only read through your error message and didn't read the last line.
" try to add (hObject, eventdata, handles) to result function, but it doesn`t help. "
If adding that didn't help, I don't know that my solution would help either because extra inputs shouldn't matter.
  1 Comment
Eugene
Eugene on 2 Dec 2014
Yeah, whenI was adding
(hObject, eventdata, handles) to result function, but it doesn`t help.
I forget, that I need to add handles to function calling in
function slider1_Callback(hObject, eventdata, handles)
result;
So I add handles, and the right variant is:
function slider1_Callback(hObject, eventdata, handles)
result (handles);
function slider2_Callback(hObject, eventdata, handles)
result (handles);
function slider3_Callback(hObject, eventdata, handles)
result (handles);
function result (handles)
x=get(handles.slider1,'Value'); %position of X slider
set(handles.text1,'String', num2str(x));
y=get(handles.slider2,'Value'); %position of Y slider
set(handles.text2,'String', num2str(y));
z=get(handles.slider3,'Value'); %position of Z slider
set(handles.text3,'String', num2str(z));
result=x+y+z;
set(handles.textResult,'String', num2str(result));
Thank you for help.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by