是否有一种方法可以指定在Matlab中发生错误时运行的代码?谷歌我遇到了RunTimeErrorFcn和daqcallback,但我相信它们是特定于数据采集工具箱的。当我遇到一个bug时,我想要一些东西,比如访问一个未赋值的变量。(我使用一个名为PsychToolbox的库,它接管了图形处理器,所以我希望在返回命令提示符之前能够清除它的屏幕。)
发布于 2010-06-10 07:09:13
一个技巧是通过发出以下命令来使用Error Breakpoints:
dbstop if error使能时,会使MATLAB在出错时进入调试模式。您可以从主工具栏上的Debug菜单访问相同的功能。

发布于 2010-06-10 09:37:39
如果将代码包装在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发布于 2013-04-03 22:58:57
如果有人使用GUI并希望进行“全局”错误检测,那么解决方案可能如下所示……
function varargout = Program(varargin)
try
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @program_OpeningFcn, ...
'gui_OutputFcn', @program_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
catch exception
beep
h = errordlg('Unexpected error, the program will be restarted.','Syntax
error','modal');
uiwait(h)
Program
endhttps://stackoverflow.com/questions/3010252
复制相似问题