Functions calling back function

1 回表示 (過去 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 Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by