This code…
function [] = foo()bar();baz(); function [] = bar() a = 1; end function [] = baz() if a fprintf('Foo!\n'); end endend
…gives me this error…
Undefined function or variable 'a'.Error in foo/baz (line 12) if aError in foo (line 5)baz();
…but this code…
function [] = foo()bar();a;baz(); function [] = bar() a = 1; end function [] = baz() if a fprintf('Foo!\n'); end endend
…gives me…
Foo!
Please explain what is going on here. Is there any documentation for this behavior? I would appreciate any documentation which can help me better understand Matlab's lexical scoping rules, and how symbols are imported into certain namespaces.
Thanks.
Best Answer