Is there a way to specify code to be run whenever an error occurs in Matlab? Googling I came across RunTimeErrorFcn and daqcallback, but I believe these are specific to the
If you wrap your code in TRY/CATCH blocks, you can execute code if an error occurs, which can be customized depending on the specific error using the MEXCEPTION object.
try
% do something here
catch me
% execute code depending on the identifier of the error
switch me.identifier
case 'something'
% run code specifically for the error with identifier 'something'
otherwise
% display the unhandled errors; you could also report the stack in me.stack
disp(me.message)
end % switch
end % try/catch