Retrieving data from slider

I've been using GUIDE and I'm having trouble getting the value from the slider.
In the generated function
function slider1_Callback(hObject, eventdata, handles)
I can get the value with
sliderValue = get(handles.slider1,'Value')
But if I try using that in a user defined function I get this error:
??? Undefined variable "handles" or
class "handles.slider1".
Did I miss something?

 Accepted Answer

In your user defined function doesn't accept the same three arguments, you will get this error. .
.
.
EDIT
Open the GUI with GUIDE. Double click on the slider to open the property editor. In the callback field, put this:
fun(gcbo,[],guidata(gcbo))
Where fun is the name of your function. fun will have to take three arguments just like shown:
function fun(hObject, eventdata, handles)

2 Comments

t
t on 2 Apr 2011
Do you mean like this?
function fun(hObject, eventdata, handles)
sliderValue = get(handles.slider1,'Value')
This gives me
??? Input argument "handles" is
undefined.
How are you assigning this callback to the slider?

Sign in to comment.

More Answers (1)

t
t on 2 Apr 2011
Like I posted in the comment above, I tried putting the same arguments in the function parameters
function fun(hObject, eventdata, handles)
sliderValue = get(handles.slider1,'Value')
and I get
??? Input argument "handles" is
undefined.

4 Comments

But how are you telling the GUI to call your function fun? And where is fun, in the GUI M-file, or outside?
t
t on 2 Apr 2011
I've tried using fun() in the slider itself and on a button press just to test it.
fun() is in the GUI .m file, I'm doing everything in the same GUIDE generated .m file.
See my above answer...
t
t on 2 Apr 2011
I guess that works but it's really not an appropriate solution. The data needs to be accessed by several functions, all called using the GUI at different times.
I will have to use global variable as I've been wasting a lot of time trying to work out how data is stored and retrieved, which I have to say is rather unintuitive and difficult to do (for such a simple process).
Thanks a lot for helping.

Sign in to comment.

Categories

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

Tags

Asked:

t
t
on 2 Apr 2011

Community Treasure Hunt

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

Start Hunting!