When the function that splitapply throws an error, this line number of this error is not include in the output (matlab2024b)
Example:
T=table([1:3]');
G=findgroups(T);
splitapply(@fun, T, G);
function out = fun(varargin)
ERROR
end
This returns:
Error using splitapply>localsplitandapply (line 196)
Unable to apply the function 'fun' to the 1st group of data.
Error in splitapply (line 135)
varargout = localsplitandapply(fun,dataVars,gdiffed,sgnums,gdim,nargout);
Error in templateLoopOverAll (line 231)
splitapply(@fun, T, G);
Caused by:
Unrecognized function or variable 'ERROR'.
I was expeting that the line number that actually caused that error would be included:
Error in testfile>fun (line 5)
ERROR
Error in testfile (line 3)
fun()
Missing this information makes the debugging of fun very challenging.
How to retrieve this line number?

댓글 수: 1

Vasco
Vasco 2026년 1월 15일
I think that this data is removed in splitapply.m line 243.

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

 채택된 답변

Vasco
Vasco 2026년 1월 28일

0 개 추천

The solution is to alter in splitapply.m line 242 from:
throwAsCaller(addCause(MException(m.Identifier,getString(m)),ME));
to:
rethrow(addCause(ME, MException(m.Identifier,getString(m))));

추가 답변 (1개)

Matt J
Matt J 2026년 1월 15일
편집: Matt J 2026년 1월 15일

0 개 추천

Run with Pause on Errors.
Then the code will stop at the offending line:

댓글 수: 6

Matt J
Matt J 2026년 1월 15일
Youc an also invoke Pause on Errors from the command line with,
>> dbstop if error
Vasco
Vasco 2026년 1월 19일
편집: Vasco 2026년 1월 19일
dbstop if caught error
That works!
From the menu it does not work:
  • This does not work on my installation, it stops at splitapply.m line 196
  • That seems not to work when I press: run this section (crtl-enter) -> it does not stop at al
Try
dbstop if caught error
Matt J
Matt J 2026년 1월 19일
편집: Matt J 2026년 1월 19일
dbstop if error should be sufficient in R2024b when the script is run in full. My demo above used that.
The other observations are intended behavior.
Vasco
Vasco 2026년 1월 28일
That indeed halts the debugger at this place, but it also halts the debugger in many other places (for example in some of the plotting functions). This makes it hardly usable.
Matt J
Matt J 2026년 1월 28일
편집: Matt J 2026년 1월 28일
I use it all the time and have never had a problem with it stopping in plotting functions.
'dbstop if caught error' is usually more prone to stop in places you don't want, and will fail to stop at explicit errors that you do want to intercept.

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

카테고리

도움말 센터File Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

제품

릴리스

R2024b

질문:

2026년 1월 15일

편집:

2026년 1월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by