Inconsistent behavior between Command Window and scripts, unexpected error using 'end' and 'max'
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Rik
2018-8-26
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.
采纳的回答
Lola Davidson
2018-8-27
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!