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?

 采纳的回答

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 个评论

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?

请先登录,再进行评论。

更多回答(1 个)

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 个评论

But how are you telling the GUI to call your function fun? And where is fun, in the GUI M-file, or outside?
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...
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.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by