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?

 Réponse acceptée

Matt Fig
Matt Fig le 2 Avr 2011

0 votes

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 commentaires

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

Connectez-vous pour commenter.

Plus de réponses (1)

t
t le 2 Avr 2011

0 votes

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 commentaires

Matt Fig
Matt Fig le 2 Avr 2011
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 le 2 Avr 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.
Matt Fig
Matt Fig le 2 Avr 2011
See my above answer...
t
t le 2 Avr 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Interactive Control and Callbacks dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by