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
추가 답변 (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!