MATLAB: Unknown behavior of ceil() function (Matlab 2017b)

ceilceil behaviorMATLABnot enough input arguments

Dear Matlab community,
I have ran into an issue while using the ceil()-function and it results in an "Not enough input arguments" error.
I find it difficult to described behavior because I cannot find the logic behind it, but maybe I'm missing something. So I prepared three examples to demonstrate.
The question is: Why does Variant 3 not work?
You can even "pause/debug" Variant 3 in those comment lines before the last call (uncomment disp() first) and copy the last two lines with the last call into the command window and it works. If you than continue it still results in an error.
Variant1: Works and ceil() is called three times. First with the keyword "end", then with the variable "x" and again with the keyword "end".
clear all
%% Variant 1
x = 4; y = 5; Test = zeros(x,y);
MAGIC1 = magic(6);
RESULT_1 = MAGIC1(ceil(end/2),:) % Keyword end

Test(ceil(x/2),2) = 1; % Same as in variant 2, Variable x
MAGIC2 = magic(6);
RESULT_2 = MAGIC2(ceil(end/2),:) % Keyword end
Variant2: Works as well, but the first call is commented, the second is changed to be called with keyword "end". The third call remains unchanged.
clear all
%% Variant 2
x = 4; y = 5; Test = zeros(x,y);
% MAGIC1 = magic(5); % not needed in variant 2
% RESULT_1 = MAGIC1(ceil(end/2),:) % not needed in variant 2
Test(ceil(end/2),2) = 1; % to index x, x changed to end
MAGIC2 = magic(6);
RESULT_2 = MAGIC2(ceil(end/2),:)
Variant3: This one results in an error. The first call is again commented (but by uncommenting it you can make it work, (lines 5,6)). The second call is by variable x. and the third on stays the same as before (keyword end)
clear all;
%% Variant 3
x = 4; y = 5; Test = zeros(x,y);
% MAGIC1 = magic(5); % by commenting these lines
% RESULT_1 = MAGIC1(ceil(end/2),:) % line 14 results in error
Test(ceil(x/2),2) = 1; % same as in variant1
% disp('Debug here and copy following lines to workspace should work as well')
% Continue to run and it will result in "Not enough input arguments."
MAGIC2 = magic(5);
RESULT_2 = MAGIC2(ceil(end/2),:)
Thanks to all who help me unpuzzle this
Hanns

Best Answer

You can't use ceil(end/2). Even though the result of ceil is going to be used as an index, that index cannot be created from ceil(end/2) because it doesn't know what "end" is, even though you might think it should since the "end" is also inside a matrix reference. To fix, replace "end" by something, like size(MAGIC2, 1) or 3 or something:
RESULT_2 = MAGIC2(3,:)