Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conversion of warnings to errors in MATLAB

In some of my functions I want to convert some warnings into errors. For example, if I want to throw an error when str2func yields a MATLAB:str2func:invalidFunctionName warning, I would do the following:

invalid_func_id = 'MATLAB:str2func:invalidFunctionName';
%hide warning of interest
warning('off', invalid_func_id);
%this might yield the warning of interest
predicate_func_try = str2func(predicate_func);
[~, warn_id] = lastwarn;
assert(~strcmp(warn_id, invalid_func_id)...
    , 'MyFunc:InvalidFunctionName'...
    , 'The predicate function %s does not have a valid name'...
    , predicate_func...
    );
warning on all

This works fine if I know that a particular block of code can give a small set of warnings. However it is verbose and probably doesn't scale to larger code blocks. Is there a better way of doing this? Ideally I would want a function which can turn certain warnings to errors in a whole block. It would allow me to modify my example to:

warnings2errors('MATLAB:str2func:invalidFunctionName');
predicate_func_try = str2func(predicate_func);
warnings2errors('off');
like image 606
AE426082 Avatar asked Feb 24 '12 13:02

AE426082


People also ask

How do I get rid of warnings in MATLAB?

You can suppress all warning messages using the 'off' state option, with the argument 'all' in place of a message identifier. You can suppress only the last warning message in a similar manner, replacing 'all' with 'last'.

What are the three types of errors in MATLAB?

There are two types of errors that appear in MATLAB expressions: syntax errors and runtime errors. Together, we generically refer to these errors as being bugs in the MATLAB code. The debugging process is the procedure of finding these bugs and fixing them.

How do you write error messages in MATLAB?

Throw a formatted error message with a line break. You must specify more than one input argument with error if you want MATLAB to convert special characters (such as \n ) in the error message. Include information about the class of variable n in the error message.

How do you find errors in MATLAB?

Select MATLAB > Code Analyzer, and then select the Enable integrated warning and error messages check box. Set the Underlining option to Underline warnings and errors . When continuous code checking is enabled, MATLAB displays warning and error messages about your code in the Editor and Live Editor.


2 Answers

It was mentioned as an edit by jHops, but this technique is so simple, it deserves its own concise answer.

As (un)documented in https://undocumentedmatlab.com/articles/trapping-warnings-efficiently, to turn warnings to errors, simply do

s = warning('error', 'MATLAB:nearlySingularMatrix');

where the second string is the warning ID itself. It can be found by warning on verbose in cmdline, and inspecting the warning message. In my experience, it can be further debugged by turning on dbstop if error; dbstop if warning; dbstop if naninf, depending on your issue.

This is a full example for inverting a matrix and checking if it's singular. The last line turns the warning-errors back to regular warnings. s can be either a single warning state as above, or an array.

s = [warning('error', 'MATLAB:nearlySingularMatrix'), warning('error', 'MATLAB:singularMatrix')];
try
  Minv = inv(Mat);
  InvertFail = false;
catch
  InvertFail = true;
  return;
end
warning(s);
like image 150
JWCS Avatar answered Oct 07 '22 11:10

JWCS


I'm not aware of a clean way to do exactly what you want. Depending on your reason for wanting to turn errors into warnings, you may be able to get by with:

dbstop if warning

or

dbstop if warning MyFunc:InvalidFunctionName

You can also look at warning on stacktrace, to get more infomrat6ion on warnings as they appear.

If you need an actual error message (not just a way to break into the debugger) then I'm actually pretty impressed with the method you included in the question.

like image 32
Pursuit Avatar answered Oct 07 '22 09:10

Pursuit