Conditional function handle implementation blocks even when not executed?

1 view (last 30 days)
I'm trying to understand function handles
function foo(test)
if test
sqrt = @(x) x.^3;
end
disp(sqrt(3))
The redefinition of sqrt (analogous to something needed in a more complicated case) appears to be somehow "blocking". If the conditional is false, it blocks the standard definition of sqrt without actually implementing the substitute.
>> foo(0)
Undefined function or variable "sqrt".
Error in foo (line 5)
disp(sqrt(3))
>> foo(1)
27
>> sqrt(10)
ans =
3.1623
I understand there are work-arounds for this, but was wondering why this is?
Tested in R2012b and R2014a on Mac OS X 10.10.3.

Answers (0)

Categories

Find more on Modeling in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!