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
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.
Joseph DelPreto
Joseph DelPreto 2018년 9월 25일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Lola Davidson
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
  댓글 수: 1
Joseph DelPreto
Joseph DelPreto 2018년 9월 25일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by