Controller
Controller

Reputation: 529

Variables scope in MATLAB

I have the following code :

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

end

I thought it would display 'success', but it does not! Why is that? Does it have to do with variables scope?Is there a workaround?

Upvotes: 0

Views: 67

Answers (1)

Razer
Razer

Reputation: 8211

Your inner function is never called. Try this, and success is displayed:

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

Upvotes: 3

Related Questions