- dbstop if error
- dbstop if caught error
- dbstop if warning
- variants with specific message ID
- dbstop if naninf
dbclear all s.fred = 1; dbtype isfieldTryE
1 function tf = isfieldTryE(s,f) 2 %ISFIELD True if field is in structure array. 3 % F = ISFIELDTRYE(S,'field') returns true 4 % if 'field' is the name of a field 5 % in the structure array S. 6 % 7 % See also GETFIELD, SETFIELD, FIELDNAMES. 8 9 tf = true; 10 try 11 tmp = s.(f); 12 catch E 13 % I don't care why it failed. 14 tf = false; 15 end 16 17
dbstop if error isfieldTryE(s,'fred')
ans = 1
ans = 0What you can see is that testing for either an existing or non-existent field sails on through. And I get the same behavior if I try to stop only for an appropriate message identifier.
dbstop if error MATLAB:nonExistentField isfieldTryE(s,'fred') isfieldTryE(s,'freddy')
ans = 1 ans = 0
dbstop if caught error MATLAB:nonExistentField isfieldTryE(s,'fred') isfieldTryE(s,'freddy')
ans = 1 Caught-error breakpoint was hit in isfieldTryE at line 11. The error was: Reference to non-existent field 'freddy'. ans = 0Now when I ask for a non-existent field, the debugger stops in the catch block. And to resume, I use
- breakpoints disappearing when debugging. This is often caused by a clear all in code being run. See, for example, this post.
- name collisions. These are caused either by using a variable name the same as a name for a MATLAB function, or having multiple functions with the same name. Using which can help you figure out what's going on if you realize there's a nameclash.
- variables not being present. This is often an issue of not understanding scoping in MATLAB, including inputs and outputs of functions, what workspace graphics callbacks execute in, scripts versus functions.
Get the MATLAB code Published with MATLAB® 7.5
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.