2014-08-31 26 views
0

我有以下代碼:變量的作用域在MATLAB

function test 
s1=1; 
s2=-1; 
    function inner_test 
     s2=1; 
     if s1==s2 
      display('success') 
     end 
    end 

end 

我認爲它會顯示「成功」,但事實並非如此!這是爲什麼?它是否與變量範圍有關?是否有解決方法?

回答

3

你的內在功能永遠不會被調用。試試這個,success顯示:

function test 
    s1 = 1; 
    s2 = -1; 
    function inner_test 
    s2 = 1; 
    if s1 == s2 
     display('success') 
    end 
    end 
    inner_test() 
end