Conditional function handle implementation blocks even when not executed?
1 view (last 30 days)
Show older comments
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.
1 Comment
Walter Roberson
on 27 May 2015
Interesting.
Question: is there an "end" matching the "function" statement in the actual code?
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!