slider callback

[EDIT: 201107`5 20:23 CDT - reformat - WDR]
hi all
i want to obtain the output of value a from callback function of slider
i used
h=uicontrol('Style', 'slider',...
'Min',0,'Max',1,'Value',0.01,...
'Position', [400 20 120 20],...
'Callback',{@su});
function a= su(h,event) %#ok<INUSL>
val =get(h,'Value');
a=val
end
i want the value after changing the slider position by mouse and i used:
d=su(h,'Value')
but it return the initial value of slider
thanks

Answers (2)

Fangjun Jiang
Fangjun Jiang on 16 Jul 2011
No, I tried your code and it worked fine. Maybe:
1. You don't need the "end" statement
2. You need to save the function su() in a separate .m file as su.m
3. Or, add a line at the begining "function mygui" and save it as mygui.m and then run it.

6 Comments

mohamed khalaf
mohamed khalaf on 16 Jul 2011
no fangjun
u didn't understand me try this
function ex h=uicontrol('Style', 'slider',... 'Min',0,'Max',1,'Value',0.01,... 'Position', [400 20 120 20],... 'Callback',{@su}); d=su(h,'Value')
end
function a= su(h,event) %#ok<INUSL>
val =get(h,'Value');
a=val
end
i want to obtain the value of slider after changing and call this value in another function or i want obtain the value of a in callback function
thanks
Comment on this Answer
Fangjun Jiang
Fangjun Jiang on 16 Jul 2011
Your function ex() is to create the slider. It only needs to be executed once. Your d=su(h,'Value') statement inside the function ex() will of course get its initial value. If you move the slider, you will see different values appear in command window. That is because every time you move the slider, the callback function su() is executed. Inisde su(), you have a=val statement without the semi-column. That indicates your slider is working. If you want to get the slider value some where else, just call d=su(h,'Value') or just use this statement d=get(h,'Value').
mohamed khalaf
mohamed khalaf on 17 Jul 2011
okay fangjun
but i want to be sure that the value of slider was changed
i try this code but not work in the function ex()
but done in callback function su()
as u see
function exx
uicontrol('Style','text',...
'Position',[400 45 120 20],...
'String','Vertical Exaggeration')
h=uicontrol('Style', 'slider',...
'Min',0,'Max',1,'Value',0.01,...
'Position', [400 20 120 20],...
'Callback', {@su});
d=su(h,'Value');
j=imread('hand1.jpg');
in=imadjust(j, [0.0 0.8], [d 0.5]);
subplot(1,2,1),imshow(in);
end
function a=su(h,event) %#ok<INUSL>
val =get(h,'Value');
a=val
j=imread('hand1.jpg');
in=imadjust(j, [0.0 0.8], [a 0.5]);
subplot(1,2,2),imshow(in);
end
okay i want to done in function ex() not in su()
i want to get the value of a in su() and store it in d in function ex()
after each move of slider and changing the value of a
thanks
Fangjun Jiang
Fangjun Jiang on 17 Jul 2011
Remember, your function exx only executes once. Every time you move the slider, function su() is called. That is what your specified in the 'Callback',{@su} statement. Why do you have to do it in exx? You need to understand how GUI works. In one function, you set up your GUI, text box, slider, axes for image, etc. If any of the GUI has event, slider got moved, button got pushed, etc. the corresponding callback function will be called. That is where you put your functionality in. If you need a proper initial value for the slider in exx, you can set its value when you create it. Instead of 0.01, you can specify any value.
mohamed khalaf
mohamed khalaf on 17 Jul 2011
okay no way to call the value of a in exx()?
Fangjun Jiang
Fangjun Jiang on 17 Jul 2011
You can call it but it's pointless, right? You just set its value to be 0.01 and then call it right away to get the value. Function exx() executes only once. Once the Slider is created, the code inside exx() is not longer executed.

Sign in to comment.

Walter Roberson
Walter Roberson on 16 Jul 2011
Seems a bit pointless.
h=uicontrol('Style', 'slider',...
'Min',0,'Max',1,'Value',0.01,...
'Position', [400 20 120 20]);
su = @() get(h,'Value');
Then, whenever you want the value, call su() -- having made sure that su is in scope for the place it is called.

3 Comments

mohamed khalaf
mohamed khalaf on 17 Jul 2011
okay walter
but it store the initial value of slider
not store the new value after changing
thanks
The code I gave doesn't store anything. Calling su() will always give the current value of the slider. If you are not seeing the current value of the slider in your code, perhaps your code is not waiting for the slider change before it extracts the slider value.
mohamed khalaf
mohamed khalaf on 17 Jul 2011
okay i run your code
but the same problem appear again using su()the initial value is appear
thanks

Sign in to comment.

Categories

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

Asked:

on 16 Jul 2011

Community Treasure Hunt

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

Start Hunting!