Cannot get to work a callback function in a simple uicontrol!!!!

16 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Walter Roberson
Walter Roberson el 30 de Mayo de 2013
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.

Más respuestas (1)

Giorgos Papakonstantinou
Giorgos Papakonstantinou el 30 de Mayo de 2013
Editada: Giorgos Papakonstantinou el 30 de Mayo de 2013
Thank you Walter for the answer! It was very nice your suggestion

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by