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!