Inconsistent behavior between Command Window and scripts, unexpected error using 'end' and 'max'
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Joseph DelPreto
il 26 Ago 2018
Commentato: Joseph DelPreto
il 25 Set 2018
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 Commenti
Rik
il 26 Ago 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.
Risposta accettata
Lola Davidson
il 27 Ago 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
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Startup and Shutdown in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!