lexical scoping and namespaces
Show older comments
This code...
function [] = foo()
bar();
baz();
function [] = bar()
a = 1;
end
function [] = baz()
if a
fprintf('Foo!\n');
end
end
end
...gives me this error...
Undefined function or variable 'a'.
Error in foo/baz (line 12)
if a
Error 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
end
end
...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.
Accepted Answer
More Answers (0)
Categories
Find more on Workspace Variables and MAT Files in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!