Functions calling back function

3 次查看(过去 30 天)
IO2000
IO2000 2013-6-14
The followwing code should shows the value of "a" when the user press the submenue "ADD or "MULT". but it does not. Why?
if true
function mygui5c
%creating the figure
f = figure('visible','on','Position',[350,100,650,500],'color', [0.25,0.83,0.83]);
i_menu = uimenu('Label','My Menu');
op1_menu = uimenu(i_menu,'Label','ADD','callback',{@op1_callback});
op2_menu = uimenu(i_menu,'Label','MULT','callback',{@op2_callback});
global c
global a
c = 0;
if c ==1
a = 10
elseif c ==2
a = 100
end
function op1_callback(hObject,eventdata)
c =1;
mygui5c
end
function op2_callback(hObject,eventdata)
c=2;
end
end
end

回答(1 个)

Andrew Reibold
Andrew Reibold 2013-6-14
编辑:Andrew Reibold 2013-6-14
Because you set c to zero right before your if/else statements.
c = 0;
Then you say "If c equals 1, then a = 10"
if c ==1
a = 10
This is not true, because c = 0.
And then you say "If thats not true, but c equals 2, then a=100"
elseif c ==2
a = 100
This is also not true, because c does not equal 2... c = 0. You don't have any more 'else' statements so nothing happens and 'a' is never set.

类别

Help CenterFile 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