Calling the error function does not print the line number in r2022b
Ältere Kommentare anzeigen
In r2021a I can generate an error inside a function and the error message will be printed in the Command Window along with the stack trace and relevant line numbers:
r2021a

In r2022b, the stack trace is displayed with the error line number on the caller script, but the line number error is not displayed on the callee script:
r2022b

Please review this behavior and fix in upcoming updates.
1 Kommentar
Stephen23
am 13 Jun. 2023
"Please review this behavior and fix in upcoming updates."
Agreed.
How did this it-really-is-not-a-feature decision get past anyone who actually uses MATLAB?
Akzeptierte Antwort
Weitere Antworten (5)
William Graves
am 6 Nov. 2023
4 Stimmen
Agreed this is quite annoying. It seems like, for user-defined functions, the line number should always be displayed.
As a workaround, I've been using assert(0, ...) instead of error(...). This results in a printout like this that at least shows the line number where it occurred:

John Paden
am 13 Nov. 2023
3 Stimmen
The bug may not occur at the line where the failure occurs, but that line of code is almost ALWAYS helpful in figuring out what failed. I definitely hope Mathworks reverts this feature of removing the line number. I do not think it is helpful to remove potentially useful information and the line number is printed on the same line as the filename so it does not even clutter the output. It is so nice to be able to just click on the line number and have it jump directly to where the error occurred.
I personally use my own customized versions of error and warning. They take an options struct as the first argument to determine where the message should be printed.
options = struct('fid',1); % Print to command window.
call_a_test(options)
The intention is that the printed text is stable across all versions of Matlab and Octave. I know this is not exactly the solution you're looking for, but it might be useful to you nonetheless.
(Note that what you perceive as a bug might be an intentional design choice you disagree with.)
2 Kommentare
Sergio Roche
am 29 Sep. 2022
Rik
am 29 Sep. 2022
I will probably publish these functions to the file exchange at some point. If and when I do, there will be a test matrix included. It is indeed my goal to cover every version since Matlab 6.5 (which is 20 years old by now).
If my answer helped you, please consider marking it as accepted answer.
I don't know if you can expect a staff member to comment, unless you contact support explicitly. Most comments posted by staff members are posted under their own capacity, not as an official statement from Mathworks.
Xingwang Yong
am 24 Aug. 2023
1 Stimme
Yet another un-wanted feature. The bug report says it is fixed, but in R2023a it is still there.
1 Kommentar
Not sure if the behavior is different with scripts, but this is the current behavior on this forum:
mytest()
function mytest()
a = 1+2;
error('hello world')
end
gwoo
am 1 Mär. 2024
1 Stimme
I just ran into this this past week. I made a work around that would fprintf the same info, like the line number along with hyperlinks, before it rethrows the error message. I also submitted for support for it and shared this very page's link.
The response I got was interesting and in my environment, I'm not able to try it out but hopefully many of you can and provide the strong feedback Mathworks needs to hear:
As we work through these concerns, I would like to offer a recommendation that may provide some immediate relief. The MATLAB R2024a pre-release version has already reverted this functionality, restoring the display of line numbers when exceptions are triggered by the 'error()' function. While I cannot guarantee this feature will be present in the final R2024a release, testing the Prerelease could offer a temporary solution and an opportunity to provide further feedback on its implementation. You may follow the link below to download the R2024a Prerelease.
1 Kommentar
Ronan Fleming
am 10 Mai 2024
Anyone who writes a lot of matlab code uses a breakpoint at the line where the error is thrown to re-run the code and debug in situ before it is too late. Removing the default option to see where the error was is daft, but not as daft as at least providing an option on the error function to turn it on or off. It should be on by default. I agree with the sentiment of Stephen:
"How did this it-really-is-not-a-feature decision get past anyone who actually uses MATLAB?"
Kategorien
Mehr zu Startup and Shutdown finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!