{"id":4267,"date":"2013-02-01T09:00:31","date_gmt":"2013-02-01T14:00:31","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=4267"},"modified":"2017-01-06T21:35:58","modified_gmt":"2017-01-07T02:35:58","slug":"coding-challenge-on-input-parsing","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2013\/02\/01\/coding-challenge-on-input-parsing\/","title":{"rendered":"Coding challenge on input parsing"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27485-boundedline-m\">boundedline<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/287218\">Kelly Kearney<\/a>. Today, there's also a fun challenge at the end!\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">boundedline<\/a><\/li>\r\n         <li><a href=\"#2\">Challenge<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>boundedline<a name=\"1\"><\/a><\/h3>\r\n   <p>Kelly is a veteran of the File Exchange, with a number of great <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/index?term=authorid%3A287218\">entries<\/a>, many of which have high ratings. I realized I had already <a href=\"https:\/\/blogs.mathworks.com\/pick\/2011\/06\/24\/flexible-legends\/\">picked<\/a> one of her entries, <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/31092-legendflex-m--a-more-flexible--customizable-legend\"><tt>legendflex<\/tt><\/a>. Going back and reading that post and looking through her code for <tt>legendflex<\/tt>, I realized why I like her entries. Kelly's programs are\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>well-written, with great help.<\/li>\r\n         <li>flexible, with many options.<\/li>\r\n         <li>robust, with good error checking.<\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>Just type<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">help <span style=\"color: #A020F0\">boundedline<\/span><\/pre><p>and you'll see what I mean. <tt>boundedline<\/tt> is a nice function that allows you to plot lines with shaded bounds, that can represent error or confidence bounds.\r\n   <\/p>\r\n\r\n   <p>She acknowledges a few other File Exchange entries, a couple of which have been picked previously as a Pick of the Week. <tt>boundedline<\/tt> has additional capabilities that make it unique.\r\n   <\/p>\r\n   <h3>Challenge<a name=\"2\"><\/a><\/h3>\r\n   <p>But today, I'd like to focus on one aspect of this entry, namely input parsing and error checking. As you can see from the\r\n      help for the function, you can call <tt>boundedline<\/tt> in various ways:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[hl, hp] = boundedline(x, y, b)\r\n[hl, hp] = boundedline(x, y, b, linespec)\r\n[hl, hp] = boundedline(x1, y1, b1, linespec1, x2, y2, b2, linespec2)\r\n[hl, hp] = boundedline(. . ., <span style=\"color: #A020F0\">'alpha'<\/span>)\r\n[hl, hp] = boundedline(. . ., ax)\r\n[hl, hp] = boundedline(. . ., <span style=\"color: #A020F0\">'transparency'<\/span>, trans)\r\n[hl, hp] = boundedline(. . ., <span style=\"color: #A020F0\">'orientation'<\/span>, orient)\r\n[hl, hp] = boundedline(. . ., <span style=\"color: #A020F0\">'cmap'<\/span>, cmap)<\/pre><p>There are some required inputs, some optional inputs, and some optional parameter-value inputs. In order to make this work,\r\n      Kelly parses the inputs and makes sure that the inputs are all valid. But she doesn't use <a title=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/inputparserclass.html (link no longer works)\"><tt>inputParser<\/tt><\/a> or <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/validateattributes.html\"><tt>validateattributes<\/tt><\/a>. I know that she knows about these functions, because she used them in her other entry, <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/31092-legendflex-m--a-more-flexible--customizable-legend\"><tt>legendflex<\/tt><\/a>. Perhaps she didn't know about them when she wrote <tt>boundedline<\/tt>.\r\n   <\/p>\r\n   <p>So my challenge to everyone is to come up with some code that would do the appropriate input parsing and error checking on\r\n      the inputs using <tt>inputParser<\/tt>. Feel free to use various error-checking functions, such as <tt>validateattributes<\/tt>.\r\n   <\/p>\r\n   <p>Now, I haven't done this myself, but I can envision for the most part how I would go about doing this. However, I'm struggling\r\n      with the 3rd calling syntax above. So I'm going to make that an extra credit. Here are the rules\/requirements:\r\n   <\/p>\r\n   <div>\r\n      <ol>\r\n         <li>The function should be able to handle all, except the 3rd, calling syntax above. Any number of the optional arguments can\r\n            be passed in.\r\n         <\/li>\r\n         <li>The input parameters should meet the respective criteria. (see help boundedline)<\/li>\r\n         <li>All optional inputs should have default values. (see help boundedline)<\/li>\r\n         <li>You can assume that the parameter-value pair inputs will be called after the other optional inputs.<\/li>\r\n         <li>After parsing, the parameters should be put into a structure with fields \"x\", \"y\", \"b\", \"linespec\", \"usealpha\", \"hax\", \"trans\",\r\n            \"orient\", \"cmap\".\r\n         <\/li>\r\n         <li>(Extra credit) Allow for the 3rd calling syntax.<\/li>\r\n      <\/ol>\r\n   <\/div>\r\n   <p>That's it! Put your thinking hat on, and post your solutions <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=4267#respond\">here<\/a> to win some MATLAB swag! I will write another post highlighting some of the solutions I get.\r\n   <\/p>\r\n   <p>Please post the code answer as a function that takes in <tt>varargin<\/tt> as an input and returns <tt>strct<\/tt> (with appropriate fields) as an output. Be sure to use the \"code\" markup so that your code will be easier to read:\r\n   <\/p><pre>&lt;pre class=\"code\"&gt;\r\nfuntionc strct = validateIntputs(varargin)\r\n  ...\r\nend\r\n&lt;\/pre&gt;<\/pre><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_6a8297e7d04b4e309e6c8b90d0849d8d() {\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='6a8297e7d04b4e309e6c8b90d0849d8d ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 6a8297e7d04b4e309e6c8b90d0849d8d';\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 = 'Jiro Doke';\r\n        copyright = 'Copyright 2013 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_6a8297e7d04b4e309e6c8b90d0849d8d()\"><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; R2012b<br><\/p>\r\n<\/div>\r\n<!--\r\n6a8297e7d04b4e309e6c8b90d0849d8d ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27485-boundedline-m boundedline>\r\n% by <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/287218\r\n% Kelly Kearney>. Today, there's also a fun challenge at the end!\r\n\r\n%% |boundedline|\r\n% Kelly is a veteran of the File Exchange, with a number of great\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/index?term=authorid%3A287218\r\n% entries>, many of which have high ratings. I realized I had already\r\n% <https:\/\/blogs.mathworks.com\/pick\/2011\/06\/24\/flexible-legends\/ picked> one\r\n% of her entries,\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/31092-legendflex-m--a-more-flexible--customizable-legend |legendflex|>.\r\n% Going back and reading that post and looking through her code for\r\n% |legendflex|, I realized why I like her entries. Kelly's programs are \r\n%\r\n% * well-written, with great help.\r\n% * flexible, with many options.\r\n% * robust, with good error checking.\r\n%\r\n% Just type\r\n%\r\n%   help boundedline\r\n%\r\n% and you'll see what I mean. |boundedline| is a nice function that allows\r\n% you to plot lines with shaded bounds, that can represent error or\r\n% confidence bounds.\r\n%\r\n% <<https:\/\/www.mathworks.com\/matlabcentral\/fx_files\/27485\/2\/boundedlineex.png>>\r\n% \r\n% She acknowledges a few other File Exchange entries, a couple of which\r\n% have been picked previously as a Pick of the Week. |boundedline| has\r\n% additional capabilities that make it unique.\r\n\r\n%% Challenge\r\n% But today, I'd like to focus on one aspect of this entry, namely input\r\n% parsing and error checking. As you can see from the help for the\r\n% function, you can call |boundedline| in various ways:\r\n%\r\n%   [hl, hp] = boundedline(x, y, b)\r\n%   [hl, hp] = boundedline(x, y, b, linespec)\r\n%   [hl, hp] = boundedline(x1, y1, b1, linespec1, x2, y2, b2, linespec2)\r\n%   [hl, hp] = boundedline(. . ., 'alpha')\r\n%   [hl, hp] = boundedline(. . ., ax)\r\n%   [hl, hp] = boundedline(. . ., 'transparency', trans)\r\n%   [hl, hp] = boundedline(. . ., 'orientation', orient)\r\n%   [hl, hp] = boundedline(. . ., 'cmap', cmap)\r\n%\r\n% There are some required inputs, some optional inputs, and some optional\r\n% parameter-value inputs. In order to make this work, Kelly parses the\r\n% inputs and makes sure that the inputs are all valid. But she doesn't use\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/inputparserclass.html\r\n% |inputParser|> or\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/validateattributes.html\r\n% |validateattributes|>. I know that she knows about these functions,\r\n% because she used them in her other entry,\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/31092-legendflex-m--a-more-flexible--customizable-legend |legendflex|>.\r\n% Perhaps she didn't know about them when she wrote |boundedline|.\r\n%\r\n% So my challenge to everyone is to come up with some code that would do\r\n% the appropriate input parsing and error checking on the inputs using\r\n% |inputParser|. Feel free to use various error-checking functions, such as\r\n% |validateattributes|.\r\n%\r\n% Now, I haven't done this myself, but I can envision for the most part how\r\n% I would go about doing this. However, I'm struggling with the 3rd calling\r\n% syntax above. So I'm going to make that an extra credit. Here are the\r\n% rules\/requirements:\r\n%\r\n% # The function should be able to handle all, except the 3rd, calling\r\n% syntax above. Any number of the optional arguments can be passed in.\r\n% # The input parameters should meet the respective criteria. (see help\r\n% boundedline)\r\n% # All optional inputs should have default values. (see help boundedline)\r\n% # You can assume that the parameter-value pair inputs will be called\r\n% after the other optional inputs.\r\n% # After parsing, the parameters should be put into a structure with\r\n% fields \"x\", \"y\", \"b\", \"linespec\", \"usealpha\", \"hax\", \"trans\", \"orient\",\r\n% \"cmap\".\r\n% # (Extra credit) Allow for the 3rd calling syntax.\r\n%\r\n% That's it! Put your thinking hat on, and post your solutions\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=4267#respond here> to win some MATLAB\r\n% swag! I will write another post highlighting some of the solutions I get.\r\n%\r\n% Please post the code answer as a function that takes in |varargin| as\r\n% an input and returns |strct| (with appropriate fields) as an output. Be\r\n% sure to use the \"code\" markup so that your code will be easier to read:\r\n%\r\n%  <pre class=\"code\">\r\n%  funtionc strct = validateIntputs(varargin)\r\n%    ...\r\n%  end\r\n%  <\/pre>\r\n##### SOURCE END ##### 6a8297e7d04b4e309e6c8b90d0849d8d\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Jiro's pick this week is boundedline by Kelly Kearney. Today, there's also a fun challenge at the end!\r\n      \r\n   \r\n   Contents\r\n   \r\n      \r\n ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2013\/02\/01\/coding-challenge-on-input-parsing\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4267"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=4267"}],"version-history":[{"count":9,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4267\/revisions"}],"predecessor-version":[{"id":8348,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4267\/revisions\/8348"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=4267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=4267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=4267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}