{"id":59,"date":"2006-10-18T09:25:55","date_gmt":"2006-10-18T14:25:55","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=59"},"modified":"2017-09-07T11:19:53","modified_gmt":"2017-09-07T16:19:53","slug":"controlling-warning-messages-and-state","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2006\/10\/18\/controlling-warning-messages-and-state\/","title":{"rendered":"Controlling Warning Messages and State"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>There are occasional posts on the MATLAB newsgroup, such as <a href=\"\">this one<\/a>, where people ask how to control when they see various warnings.  One reason people might want this is to control what a\r\n         user sees during some calculation.  For example, you may know that a particular warning is likely to crop up during the calculation,\r\n         but for the purposes of this specific calculation, it is not a worry.  You'd like to turn this particular warning off in the\r\n         context of your calculation, but not necessarily for the remainder of your MATLAB session.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Evolution of Warnings<\/a><\/li>\r\n         <li><a href=\"#3\">Poor Programming Pattern<\/a><\/li>\r\n         <li><a href=\"#10\">Better Code : First Option<\/a><\/li>\r\n         <li><a href=\"#12\">Better Code : Second Option<\/a><\/li>\r\n         <li><a href=\"#14\">Summary<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Evolution of Warnings<a name=\"1\"><\/a><\/h3>\r\n   <p>In older versions of MATLAB, you couldn't control the warnings at all. We followed this with the ability to control seeing\r\n      warnings or not, an all or nothing deal.  So <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/warning.html\"><tt>warning<\/tt><\/a> had a global state.  More recently, we added message identifiers and the ability to control warnings more finely.  We introduced these message ids was specifically so you could control the messages you see.  In addition, they\r\n      help us supply translated messages for the Japanese version of MATLAB on Windows.\r\n   <\/p>\r\n   <p>Capture warning state for later use.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">sw = warning;<\/pre><h3>Poor Programming Pattern<a name=\"3\"><\/a><\/h3>\r\n   <p>Let me illustrate one of the programming patterns I've seen.  Here's a line of code that normally gives a warning in MATLAB\r\n      R2006b:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">1\/0<\/pre><pre style=\"font-style:oblique\">Warning: Divide by zero.\r\nans =\r\n   Inf\r\n<\/pre><p>Suppose I don't want to see that message.  I can turn all warnings off and then enable them after I finish.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">warning <span style=\"color: #A020F0\">off<\/span>\r\n1\/0\r\nwarning <span style=\"color: #A020F0\">on<\/span><\/pre><pre style=\"font-style:oblique\">ans =\r\n   Inf\r\n<\/pre><p>The reason this is problematic is that I may have some other warnings in some non-default state.  But by typing <tt>warning on<\/tt>, I have obliterated that information.  Let me look at the state of warnings now:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">swn = warning<\/pre><pre style=\"font-style:oblique\">swn = \r\n2x1 struct array with fields:\r\n    identifier\r\n    state\r\n<\/pre><p>We can see there are two elements to my warning state and they are<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">swn(1)\r\nswn(2)<\/pre><pre style=\"font-style:oblique\">ans = \r\n    identifier: 'all'\r\n         state: 'on'\r\nans = \r\n    identifier: 'MATLAB:nonScalarConditional'\r\n         state: 'off'\r\n<\/pre><p>Compare this to the original warning state I saved at the beginning of this article.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">sw<\/pre><pre style=\"font-style:oblique\">sw = \r\n8x1 struct array with fields:\r\n    identifier\r\n    state\r\n<\/pre><p>The original settings were<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">for<\/span> ind = 1:length(sw)\r\n    sw(ind)\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">ans = \r\n    identifier: 'all'\r\n         state: 'on'\r\nans = \r\n    identifier: 'MATLAB:intConvertNonIntVal'\r\n         state: 'off'\r\nans = \r\n    identifier: 'MATLAB:intConvertNaN'\r\n         state: 'off'\r\nans = \r\n    identifier: 'MATLAB:intMathOverflow'\r\n         state: 'off'\r\nans = \r\n    identifier: 'MATLAB:intConvertOverflow'\r\n         state: 'off'\r\nans = \r\n    identifier: 'MATLAB:nonScalarConditional'\r\n         state: 'off'\r\nans = \r\n    identifier: 'MATLAB:mir_variable_with_script_name'\r\n         state: 'error'\r\nans = \r\n    identifier: 'MATLAB:mir_warning_unrecognized_pragma'\r\n         state: 'off'\r\n<\/pre><p>Return warning state to initial one.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">warning(sw)<\/pre><h3>Better Code : First Option<a name=\"10\"><\/a><\/h3>\r\n   <p>If you know the message id for the warning you want to suppress, simply turn that warning off.  To find the id, if it exists,\r\n      use <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/lastwarn.html\"><tt>lastwarn<\/tt><\/a>. Then set the state explicitly for that id.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">1\/0\r\n[msg, msgid] = lastwarn<\/pre><pre style=\"font-style:oblique\">Warning: Divide by zero.\r\nans =\r\n   Inf\r\nmsg =\r\nDivide by zero.\r\nmsgid =\r\nMATLAB:divideByZero\r\n<\/pre><p>Note that the state <tt>s<\/tt> captured below is the state <b>prior<\/b> to resetting the warning state.  In this way, you can preserve the original state and set to a new state in one statement.\r\n       Later, you can use <tt>s<\/tt> to restore to the original state.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s = warning(<span style=\"color: #A020F0\">'off'<\/span>,msgid);\r\n1\/0\r\nwarning(s)  <span style=\"color: #228B22\">% restore state<\/span><\/pre><pre style=\"font-style:oblique\">ans =\r\n   Inf\r\n<\/pre><h3>Better Code : Second Option<a name=\"12\"><\/a><\/h3>\r\n   <p>Not all warning messages have identifiers associated with them.  If you know of a warning you don't want to see, but it does\r\n      not have an associated id, you can do this.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s = warning(<span style=\"color: #A020F0\">'off'<\/span>,<span style=\"color: #A020F0\">'all'<\/span>); <span style=\"color: #228B22\">% turn all warnings off<\/span>\r\n<span style=\"color: #0000FF\">if<\/span> true,\r\n    disp(<span style=\"color: #A020F0\">'I am running this code!'<\/span>)\r\n    warning(<span style=\"color: #A020F0\">'Here is a warning that doesn''t have an id.'<\/span>)\r\n<span style=\"color: #0000FF\">end<\/span>\r\nwarning(s)  <span style=\"color: #228B22\">% restore the warning state<\/span><\/pre><pre style=\"font-style:oblique\">I am running this code!\r\n<\/pre><p>I turned all the warnings off so you didn't see the message.  So, did I actually trigger the warning?  Yes, and here's how\r\n      I know.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[msglast, msgidlast] = lastwarn<\/pre><pre style=\"font-style:oblique\">msglast =\r\nHere is a warning that doesn't have an id.\r\nmsgidlast =\r\n     ''\r\n<\/pre><h3>Summary<a name=\"14\"><\/a><\/h3>\r\n   <p>There are set of functions in MATLAB that allow you control the state of warnings for your work without disrupting how all\r\n      functions behave.  Any thoughts on when and how you use this set of features? <a href=\"https:\/\/blogs.mathworks.com\/loren\/2006\/10\/18\/controlling-warning-messages-and-state\/#respond\">Let me know<\/a>.\r\n   <\/p>\r\n   <p><i>Loren Shure<\/i><\/p>\r\n   <p><i>Copyright 2006 The MathWorks, Inc.<\/i><\/p>\r\n   <p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br>\r\n      Published with MATLAB&reg; 7.3<br><\/p>\r\n<\/div>\r\n<!--\r\n##### SOURCE BEGIN #####\r\n%% Controlling Warning Messages and State\r\n% There are occasional posts on the MATLAB newsgroup, such as\r\n% < this one>,\r\n% where people ask how to control when they see various warnings.  One\r\n% reason people might want this is to control what a user sees during some\r\n% calculation.  For example, you may know that a particular warning is\r\n% likely to crop up during the calculation, but for the purposes of this\r\n% specific calculation, it is not a worry.  You'd like to turn this\r\n% particular warning off in the context of your calculation, but not\r\n% necessarily for the remainder of your MATLAB session.\r\n%% Evolution of Warnings\r\n% In older versions of MATLAB, you couldn't control the warnings at all.\r\n% We followed this with the ability to control seeing warnings or not, an\r\n% all or nothing deal.  So\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/warning.html |warning|>\r\n% had a global state.  More recently, we added \r\n% <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/matlab_prog\/index.html?\/access\/helpdesk\/help\/techdoc\/matlab_prog\/f3-39493.html message identifiers>\r\n% and the ability to <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/matlab_prog\/index.html?\/access\/helpdesk\/help\/techdoc\/matlab_prog\/f3-39022.html control warnings> \r\n% more finely.  We introduced these message ids was specifically so you\r\n% could control the messages you see.  In addition, they help us supply\r\n% translated messages for the Japanese version of MATLAB on Windows.\r\n%%\r\n% Capture warning state for later use.\r\nsw = warning;\r\n%% Poor Programming Pattern\r\n% Let me illustrate one of the programming patterns I've seen.  Here's a\r\n% line of code that normally gives a warning in MATLAB R2006b:\r\n1\/0\r\n%%\r\n% Suppose I don't want to see that message.  I can turn all warnings off\r\n% and then enable them after I finish.\r\nwarning off\r\n1\/0\r\nwarning on\r\n%%\r\n% The reason this is problematic is that I may have some other warnings in\r\n% some non-default state.  But by typing |warning on|, I have obliterated\r\n% that information.  Let me look at the state of warnings now:\r\nswn = warning\r\n%% \r\n% We can see there are two elements to my warning state\r\n% and they are\r\nswn(1)\r\nswn(2)\r\n%%\r\n% Compare this to the original warning state I saved at the beginning of\r\n% this article.\r\nsw\r\n%%\r\n% The original settings were\r\nfor ind = 1:length(sw)\r\n    sw(ind)\r\nend\r\n%%\r\n% Return warning state to initial one.\r\nwarning(sw)\r\n%% Better Code : First Option\r\n% If you know the message id for the warning you want to suppress, simply\r\n% turn that warning off.  To find the id, if it exists, use \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/lastwarn.html |lastwarn|>. \r\n% Then set the state explicitly for that id.\r\n1\/0\r\n[msg, msgid] = lastwarn\r\n%%\r\n% Note that the state |s| captured below is the state *prior* to resetting\r\n% the warning state.  In this way, you can preserve the original state and\r\n% set to a new state in one statement.  Later, you can use |s| to restore to\r\n% the original state.\r\ns = warning('off',msgid);\r\n1\/0\r\nwarning(s)  % restore state\r\n%% Better Code : Second Option\r\n% Not all warning messages have identifiers associated with them.  If you\r\n% know of a warning you don't want to see, but it does not have an\r\n% associated id, you can do this.\r\ns = warning('off','all'); % turn all warnings off\r\nif true,\r\n    disp('I am running this code!')\r\n    warning('Here is a warning that doesn''t have an id.')\r\nend\r\nwarning(s)  % restore the warning state\r\n%%\r\n% I turned all the warnings off so you didn't see the message.  So, did I\r\n% actually trigger the warning?  Yes, and here's how I know.\r\n[msglast, msgidlast] = lastwarn  \r\n%% Summary\r\n% There are set of functions in MATLAB that allow you control the state of\r\n% warnings for your work without disrupting how all functions behave.  Any\r\n% thoughts on when and how you use this set of features?\r\n% <http:?p=59#respond Let me know>.\r\n%\r\n%%\r\n%\r\n% _Loren Shure_\r\n%\r\n% _Copyright 2006 The MathWorks, Inc._\r\n\r\n##### SOURCE END #####\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      There are occasional posts on the MATLAB newsgroup, such as this one, where people ask how to control when they see various warnings.  One reason people might want this is to control... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2006\/10\/18\/controlling-warning-messages-and-state\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,14,11],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/59"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=59"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/59\/revisions"}],"predecessor-version":[{"id":2433,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/59\/revisions\/2433"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}