Consider the minimally reproducible example:
f = @(x) x
f(end)
Note, the MATLAB syntax highlighter doesn't actually highlight end in blue, showing that it is not parsing as a keyword, even though the Stack Overflow syntax highlighting does.
Output:
f =
function_handle with value:
@(x)x
ans =
1
You can even do things like
f = @(x) x
a = f(end + 1)
b = f(end - 100)
c = f(2 + end)
Output:
a =
2
b =
-99
c =
3
Regular Function
If you have a regular function, this doesn't work
foo(end)
function x = foo(x)
end
Error: <filename> Line: 1 Column: 5
The end operator must be used within an array index expression.
Hypothesis
From this, it appears that end is being coerced into the value 1, similar to how the boolean/logical true gets coerced to 1. My hypothesis is that since the function handle is actually a 1x1 array of function handles, the end keyword is replaced by the last valid index of the array which is 1. Since a function is not stored as an array, it doesn't work for real functions.
My Bug
I had a particularly nasty bug where I was was trying to index into an array with a similar name as a function handle:
x = 1:5
foo = @(x) x
foos = foo(x) % [1, 2, 3, 4, 5]
out = foos(1) + 2 * sum(foos(2:end-1)) + foo(end)
% Expected: out = 1 + 2 * (2 + 3 + 4) + 5 = 24, Got: out = 1 + 2 * (2 + 3 + 4) + 1 = 20
Obviously, I renamed my variables to make them more distinguishable. Still, this behavior doesn't seem useful for anything that I can think of. Is this an artifact of result of every object (including a function handle) secretly being an array? If so, is there anything the user do to prevent this or does a fix need to come from MathWorks?
end
as an indexing expression for a 1x1 handle before the function is evaluated. It would make sense why the same doesn't work for a separate func in that sense. Perhaps raise with MathWorks and self-answer this question with the definitive answer, since we can only guess what goes on inside the interpreter – Tungstenf(1)
, the parentheses seem to be interpreted in two different ways: first as indexing into a 1x1 array of function handles, and then as a function call. The interpreter "should" pick only one (with the first interpretation it would return the anonymous function; with the second it would error). (b) The fact thatf
is a 1x1 function handle array is weird, given that it is not possible to have for example a 1x2 function handle array (it has to be a cell array of function handles) – Cardinal