{"id":89,"date":"2007-04-30T00:27:14","date_gmt":"2007-04-30T05:27:14","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2007\/04\/30\/a-little-bit-on-message-handling\/"},"modified":"2007-04-30T00:28:00","modified_gmt":"2007-04-30T05:28:00","slug":"a-little-bit-on-message-handling","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2007\/04\/30\/a-little-bit-on-message-handling\/","title":{"rendered":"A Little Bit on Message Handling"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>This is a quick note about handling errors and warnings in MATLAB. In particular, the function <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/error.html\"><tt>error<\/tt><\/a> has a couple of aspects that people sometimes trip over or overlook.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">What error Does<\/a><\/li>\r\n         <li><a href=\"#2\">Sample Snippet<\/a><\/li>\r\n         <li><a href=\"#5\">Why Use the 'struct' Option?<\/a><\/li>\r\n         <li><a href=\"#6\">Another gotcha<\/a><\/li>\r\n         <li><a href=\"#7\">Any More Message Wisdom?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>What error Does<a name=\"1\"><\/a><\/h3>\r\n   <p>The main thing <tt>error<\/tt> is responsible for is issuing a message to the user that he or she has done something incorrectly.  The first place you might\r\n      see error checking in a function is looking at the input arguments. Using <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/nargchk.html\"><tt>nargchk<\/tt><\/a> is a common way to see if there's a problem and then calling <tt>error<\/tt> afterwards.\r\n   <\/p>\r\n   <h3>Sample Snippet<a name=\"2\"><\/a><\/h3>\r\n   <p>Let's see one pattern.  Here we are checking that the number of inputs, <tt>narg<\/tt>, lies between 2 and 7.  If not, we'll issue an error.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">narg = 4;\r\nmsg = nargchk(2,7,narg,<span style=\"color: #A020F0\">'struct'<\/span>);\r\n<span style=\"color: #0000FF\">if<\/span> ~isempty(msg)\r\n    error(msg)\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><p>It turns out, this code is more complex than necessary.  We can equivalently write<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">error(nargchk(2,7,narg,<span style=\"color: #A020F0\">'struct'<\/span>));<\/pre><p>because the function <tt>error<\/tt> <b>does not<\/b> produce an error with empty input.  This is a gotcha that people occasionally run into.  You can't just use a simple statement\r\n      like <tt>error('')<\/tt> to signal an error in your code.\r\n   <\/p>\r\n   <h3>Why Use the 'struct' Option?<a name=\"5\"><\/a><\/h3>\r\n   <p>I am using the <tt>'struct'<\/tt> argument for <tt>nargchk<\/tt> so the output is a structure with both a message identifier and the message itself.  Why? Because I, as the application developer,\r\n      could choose to issue a different message or take a different action, e.g., using a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/try.html\"><tt>try-catch<\/tt><\/a> construct.  Using message identifiers for <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/warning.html\"><tt>warning<\/tt><\/a> users about something allows the users to control that specific warning. Perhaps, after seeing it once, a user might want\r\n      to turn that warning off, but would still like to be alerted of other possible issues.\r\n   <\/p>\r\n   <h3>Another gotcha<a name=\"6\"><\/a><\/h3>\r\n   <p>Users are also occasionally tripped by turning a particular warning off, running some other code, and then not expecting to\r\n      see the turned off warning showing up when they use <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/lastwarn.html\"><tt>lastwarn<\/tt><\/a> to see if something has happened.  Depending on the sequence of events, it's possible that <tt>lastwarn<\/tt> will return a message from a warning that was turned off.  <tt>warning off <i>ID<\/i><\/tt> simply turns off the display of the warning message.\r\n   <\/p>\r\n   <h3>Any More Message Wisdom?<a name=\"7\"><\/a><\/h3>\r\n   <p>If you have more message wisdom, post your thoughts <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=89\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_98eca2eaa2034f6aa05a257b23466b89() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='98eca2eaa2034f6aa05a257b23466b89 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 98eca2eaa2034f6aa05a257b23466b89';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Loren Shure';\r\n        copyright = 'Copyright 2007 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_98eca2eaa2034f6aa05a257b23466b89()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.4<br><\/p>\r\n<\/div>\r\n<!--\r\n98eca2eaa2034f6aa05a257b23466b89 ##### SOURCE BEGIN #####\r\n%% A Little Bit on Message Handling\r\n% This is a quick note about handling errors and warnings in MATLAB.\r\n% In particular, the function\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/error.html |error|>\r\n% has a couple of aspects that people sometimes trip over or overlook.\r\n%% What error Does\r\n% The main thing |error| is responsible for is issuing a message to the user\r\n% that he or she has done something incorrectly.  The first place you might\r\n% see error checking in a function is looking at the input arguments.\r\n% Using <https:\/\/www.mathworks.com\/help\/matlab\/ref\/nargchk.html |nargchk|>\r\n% is a common way to see if there's a problem and then calling |error|\r\n% afterwards.\r\n%% Sample Snippet\r\n% Let's see one pattern.  Here we are checking that the number of inputs,\r\n% |narg|, lies between 2 and 7.  If not, we'll issue an error.\r\nnarg = 4;\r\nmsg = nargchk(2,7,narg,'struct');\r\nif ~isempty(msg)\r\n    error(msg)\r\nend\r\n%%\r\n% It turns out, this code is more complex than necessary.  We can\r\n% equivalently write\r\nerror(nargchk(2,7,narg,'struct'));\r\n%%\r\n% because the function |error| *does not* produce an error with empty\r\n% input.  This is a gotcha that people occasionally run into.  You can't\r\n% just use a simple statement like |error('')| to signal an error in your\r\n% code.\r\n%% Why Use the 'struct' Option?\r\n% I am using the |'struct'| argument for |nargchk| so the output is a\r\n% structure with both a message identifier and the message itself.  Why?\r\n% Because I, as the application developer, could choose to issue a different\r\n% message or take a different action, e.g., using a\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/try.html |try-catch|>\r\n% construct.  Using message identifiers for \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/warning.html |warning|>\r\n% users about something allows the users to control that specific warning.\r\n% Perhaps, after seeing it once, a user might want to turn that warning\r\n% off, but would still like to be alerted of other possible issues.\r\n%% Another gotcha\r\n% Users are also occasionally tripped by turning a particular warning off,\r\n% running some other code, and then not expecting to see the turned off\r\n% warning showing up when they use\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/lastwarn.html |lastwarn|>\r\n% to see if something has happened.  Depending on the sequence of events,\r\n% it's possible that |lastwarn| will return a message from a warning that\r\n% was turned off.  |warning off _ID_| simply turns off the display of the\r\n% warning message.\r\n%% Any More Message Wisdom?\r\n% If you have more message wisdom, post your thoughts \r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=89 here>.\r\n##### SOURCE END ##### 98eca2eaa2034f6aa05a257b23466b89\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      This is a quick note about handling errors and warnings in MATLAB. In particular, the function error has a couple of aspects that people sometimes trip over or overlook.\r\n      \r\n   \r\n  ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/04\/30\/a-little-bit-on-message-handling\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/89"}],"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=89"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/89\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}