splitapply does not retain ME stack

37 vues (au cours des 30 derniers jours)
Vasco
Vasco le 15 Jan 2026
Modifié(e) : Matt J le 28 Jan 2026 à 13:56
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 commentaire
Vasco
Vasco le 15 Jan 2026
I think that this data is removed in splitapply.m line 243.

Connectez-vous pour commenter.

Réponse acceptée

Vasco
Vasco le 28 Jan 2026 à 13:03
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))));

Plus de réponses (1)

Matt J
Matt J le 15 Jan 2026
Modifié(e) : Matt J le 15 Jan 2026
Run with Pause on Errors.
Then the code will stop at the offending line:
  6 commentaires
Vasco
Vasco le 28 Jan 2026 à 13:01
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 le 28 Jan 2026 à 13:05
Modifié(e) : Matt J le 28 Jan 2026 à 13:56
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Debugging and Analysis dans Help Center et File Exchange

Produits


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by