JmG*_*JmG 4 error-handling matlab
有没有办法指定在Matlab中发生错误时要运行的代码?谷歌搜索我遇到了RunTimeErrorFcn和daqcallback,但我相信这些特定于数据采集工具箱.当我遇到一个bug时,我想要一些东西,比如访问一个未分配的变量.(我使用一个名为PsychToolbox的库来接管GPU,所以我希望能够在返回命令提示符之前清除它的屏幕.)
一个技巧是通过发出命令来使用错误断点:
dbstop if error
Run Code Online (Sandbox Code Playgroud)
启用时,会导致MATLAB在错误点进入调试模式.您可以从Debug主工具栏上的菜单访问相同的功能.

如果将代码包装在TRY/CATCH块中,则可以在发生错误时执行代码,可以使用MEXCEPTION对象根据特定错误自定义代码.
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
Run Code Online (Sandbox Code Playgroud)