Inconsistent behavior between Command Window and scripts, unexpected error using 'end' and 'max'

1 view (last 30 days)
I recently experienced some strange behavior related to using 'end' and 'max' as well as to inconsistencies between the Command Window and scripts; I was wondering if anyone could help elucidate what is happening. The following simplified code should recreate the issue:
unused_assignment = max(6, 12);
clear;
x = zeros(10, 1);
x(max(1, end):10) = 1;
A summary of the behavior observed using this code is below:
  • If I copy the code into the Command Window, it works as expected without any issues.
  • If I enter the code in a new script and execute it all at once (using 'Run') or execute the section as a cell (using Ctrl+Enter or 'Run Section'), Matlab gets the following error:
Error using max
Not enough input arguments.
Error in test_mystery_error (line 4)
x(max(1, end):10) = 1;
  • If I comment out the first line, i.e. the unused assignment, then it works fine without any errors when executing from a script.
  • If I copy lines 3 and 4 to the beginning of the script (so the script constitutes lines 3,4,1,2,3,4), then it works fine without any errors when executing from a script.
  • If I copy the original 4 lines into their own function, the function works fine without any errors.
The above has been observed using Matlab 2017a, 2017b, and 2018a (using a variety of Windows and Linux computers).
If you have any insights into why the Command Window and a script would yield different results, why the script gets an error, and why the unused assignments have any impact, I'd really appreciate it!
  2 Comments
Rik
Rik on 26 Aug 2018
This has probably to do with the pre-compiling that Matlab does to accelerate code execution. I know that end is implicitly replaced by the last element count of the addressed dimension, but this code does look very odd to me.
Joseph DelPreto
Joseph DelPreto on 25 Sep 2018
Thanks for the response, that's a good point - an issue related to pre-compiling might help explain why the behavior is different depending on where the code is run (such as a script or the command window). It could be interesting to learn a little more about what Matlab does behind the scenes for its various methods of execution.

Sign in to comment.

Accepted Answer

Lola Davidson
Lola Davidson on 27 Aug 2018
Hi Joseph,
Thank you for bringing this behavior to our attention. This appears to be related to a known issue. Please see the bug report link for more details:
Additionally, I have sent your comments and reproduction steps on to our development team.
Sincerely,
MathWorks Technical Support Department
  1 Comment
Joseph DelPreto
Joseph DelPreto on 25 Sep 2018
Hi Lola,
Thank you for the response and for passing along the information to the development team! The bug report that you included certainly looks related as well. It's good to know that the case is under review and may be fixed in the future.
Best,
Joseph

Sign in to comment.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!