Why Can an Anonymous Function be Defined with a Non-Existent Parameter?
Define an anonymous function with one argument (x) and one parameter (y)
f = @(x) x + y;
I’m suprised that line sails through without an error (or even a warning) even though there is no variable called y in the workspace at the time f() is defined.
Calling f results in an error (unsurprisingly)
try
f(2)
catch ME
ME.message
end
Once f() is defined, I don’t believe there is any way to subsequently define y such that f() works.
Would it be better if an error (or at least a warning) is generated at the time of function definition rather than at the time of function execution?Define an anonymous function with one argument (x) and one parameter (y)
f = @(x) x + y;
I’m suprised that line sails through without an error (or even a warning) even though there is no variable called y in the workspace at the time f() is defined.
Calling f results in an error (unsurprisingly)
try
f(2)
catch ME
ME.message
end
Once f() is defined, I don’t believe there is any way to subsequently define y such that f() works.
Would it be better if an error (or at least a warning) is generated at the time of function definition rather than at the time of function execution? Define an anonymous function with one argument (x) and one parameter (y)
f = @(x) x + y;
I’m suprised that line sails through without an error (or even a warning) even though there is no variable called y in the workspace at the time f() is defined.
Calling f results in an error (unsurprisingly)
try
f(2)
catch ME
ME.message
end
Once f() is defined, I don’t believe there is any way to subsequently define y such that f() works.
Would it be better if an error (or at least a warning) is generated at the time of function definition rather than at the time of function execution? anonymous function, undefined parameter MATLAB Answers — New Questions