Functions calling back function

1 visualizzazione (ultimi 30 giorni)
IO2000
IO2000 il 14 Giu 2013
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

Risposte (1)

Andrew Reibold
Andrew Reibold il 14 Giu 2013
Modificato: Andrew Reibold il 14 Giu 2013
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.

Categorie

Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by