Specifying a callback in Matlab after any runtime error

Is there a way to specify code to run whenever an error occurs in Matlab? Googling I came across RunTimeErrorFcn and daqcallback, but I think they are specific to the Data Acquisition Toolbox. I want something when I just go through the error, like accessing an unassigned variable. (I use the PsychToolbox library, which takes over the GPU, so I want it to be able to clear its screen before returning to the command line.)

+4
source share
3 answers

One trick is to use Error Breakpoints by running the command:

dbstop if error 

which, when turned on, causes MATLAB to enter debug mode at the point of error. You can access the same functions from the Debug menu on the main toolbar.

breakpoints_dialog

+5
source

If you complete your code in TRY / CATCH , you can execute the code if an error occurs, which can be customized to a specific one using 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 
+5
source

If someone uses a graphical interface and wants to have global error detection, the solution might look something like this:

 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 end 
0
source

Source: https://habr.com/ru/post/1312376/


All Articles