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

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

 Accepted 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,:)

6 Comments

Okay I can accept that. The "ceil(end/2)" always felt a bit whacky to me, even though it had worked for me so far. Sure I am able to use the matrix size as a reference, which has been my solution since I encountered the problem yesterday.
I'm still a bit puzzled why it worked in variant 1 and 2, but okay.
Hum, actually, matlab should know what end refers to. Since the end is inside an indexing expression, there's no ambiguity as to what it refers to (the size of the dimension being indexed).
In R2018b, variant 3 does not produce any error. So there may indeed be a bug with 2017 that has been fixed.
Ah, that is interesting and good to know. And even better that it's probably already fixed in 2018b. Thanks for letting me know that it was possibly a bug, I thought I was going crazy for a while.
end is inside a function called ceil() and it's passed to ceil() and it's ceil that looks at it, not the matrix name. I guess whether end should be evaluated and changed into a number before it's passed to any other function is a matter of opinion. It seems right now (R2018b) and in my opinion it should throw an error, like it does now. Thanks for accepting the answer!
It's not a bug - it's a feature.bmp
bugs.gif
You would expect mymatrix(1:end-1) to work. Yet, in theory, this is actually, mymatrix(1:minus(end, 1)), so the end is inside a function call. It really shouldn't complicate anything for the parser if the end is inside a function call (as long as that call can't be confused with a matrix indexing). And actually, I'm surprised that variant 3 does not work properly in R2017b. Unfortunately, I no longer have that version installed to test.
I do not want to insinuate in the slightest that you doubt me, but just for future reference, if someelse finds this thread: This is my output if I run the three variants one after the other. Concluding with the "version" command to report my Matlab release number and version.
2017b.PNG

Sign in to comment.

More Answers (0)

Products

Release

R2017b

Community Treasure Hunt

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

Start Hunting!