Cannot get to work a callback function in a simple uicontrol!!!!
1 次查看(过去 30 天)
显示 更早的评论
I have created this simple function:
function b=hide(a)
% a is gcf
hObj=uicontrol(a,'Style', 'slider',...
'Min',1,'Max',2,'Value',2,...
'Position', [400 20 120 20],'Callback',@test);
axis tight
k=get(hObj,'Value');
b=test(k);
function [a]=test(val)
% val=get(hObj,'Value');
if val==1
set(findobj('Tag','plota'),'visible','on');
set(findobj('Tag','plotb'),'visible','off');
elseif val==2
set(findobj('Tag','plota'),'visible','off');
set(findobj('Tag','plotb'),'visible','on');
else
set(findobj('Tag','plota'),'visible','on');
set(findobj('Tag','plotb'),'visible','on');
end
a=val;
I want to hide or reveal two plots according to the value of the slider and also receive the value of the slider but I get an error:
Error while evaluating uicontrol Callback
when I call the hide(gcf) from another m file.
0 个评论
采纳的回答
Walter Roberson
2013-5-30
Callbacks for uicontrols automatically have two parameters added to the beginning of the argument list: the object being worked on, and "event" data about it. You have only defined a single parameter as being valid.
If you were to extend the argument list for "test" to
function test(val, event)
then that part would not fail, but the argument being passed in for "val" would be the object, not the value "k" that you created in "hide".
Your statement
k=get(hObj,'Value');
inside hide() is going to be executed immediately after the uicontrol gets created, and so would pass the initial Value of the uicontrol to the routine "test"; that get() is not going to be delayed until the callback is triggered. There can be good reasons to call a callback within the routine that creates the control, but when you do so you need to pass in parameters just as if the callback had been triggered by the user.
I suggest you have a look at http://www.mathworks.com/matlabcentral/fileexchange/24861-41-complete-gui-examples
更多回答(1 个)
另请参阅
类别
在 Help Center 和 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!