{"id":560,"date":"2012-02-13T10:45:02","date_gmt":"2012-02-13T15:45:02","guid":{"rendered":"https:\/\/blogs.mathworks.com\/desktop\/?p=560"},"modified":"2012-02-13T10:45:02","modified_gmt":"2012-02-13T15:45:02","slug":"parsing-inputs","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2012\/02\/13\/parsing-inputs\/","title":{"rendered":"Parsing Inputs"},"content":{"rendered":"<p>Have you ever wondered how MATLAB functions make sense of variable function arguments? Many MATLAB functions take multiple optional arguments, or arguments specified as param-value pairs. A param-value pair is usually supplied with a string parameter name, such as &#8216;Position&#8217; or &#8216;Color&#8217; followed by the value of that parameter such as [0 0 1 1] or &#8216;on&#8217; or some such. You may have seen this paradigm with the graphics functions.<\/p>\n<p>With the help of the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/inputparser.html\" target=\"_blank\"><tt>inputParser<\/tt><\/a> class, it&#8217;s easy to add param-value pairs to your own functions. This class has two great uses: (1) validation of function inputs and (2) collation of all those inputs into an easy-to-use struct.<\/p>\n<p>InputParser is used in three easy steps:<\/p>\n<ol>\n<li>Create an InputParser instance<\/li>\n<li>Specify all the inputs. Three input types are supported:\n<ul>\n<li>Required Inputs. These must always be supplied when calling your function, otherwise there is an error.<\/li>\n<li>Optional Inputs. These can be left off if desired.<\/li>\n<li>Param-Value pairs. These require a paramater name and value.<\/li>\n<\/ul>\n<\/li>\n<li>Parse the inputs and read the results.<\/li>\n<\/ol>\n<p><strong>Create an InputParser<\/strong><br \/>\nThis step is easy. Just create a new instance.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">p = inputParser<\/pre>\n<pre style=\"font-style: oblique;\">p = \r\n\r\nInput Parser object with:\r\nCaseSensitive: false\r\nStructExpand : true\r\nKeepUnmatched: false\r\nFunctionName : ''<\/pre>\n<p><strong>Specify All The Inputs<\/strong><br \/>\nOnce you have an input parser object, you need to specify each input that you want to parse. In the follow example I add one required, one optional, and one param-value pair parameter. For each input you have to specify a validation function. In these examples I&#8217;ve provided an in-line anonymous function or function handle. A handy approve-all function, if you don&#8217;t care about validation, is &#8220;<tt>@(x) true<\/tt>.&#8221;<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\"><span style=\"color: #228b22;\">% add required needs the parameter name and a validator anonymous function<\/span>\r\np.addRequired(<span style=\"color: #a020f0;\">'mainVector'<\/span>,@(x) length(x)&gt;1);\r\n<span style=\"color: #228b22;\">% add optional needs the name, default value, and validator<\/span>\r\np.addOptional(<span style=\"color: #a020f0;\">'ntimes'<\/span>,1,@isscalar);\r\n<span style=\"color: #228b22;\">% add paramValue needs the parameter name, default value, and validator<\/span>\r\np.addParamValue(<span style=\"color: #a020f0;\">'title'<\/span>,<span style=\"color: #a020f0;\">'Default title'<\/span>,@isstr);<\/pre>\n<p><strong>Run the Parser on the inputs<\/strong><br \/>\nThe next step is to actually use the <tt>inputParser<\/tt> object with a function&#8217;s input. In this exmaple example I&#8217;m using it to illustrate how the object parses different combinations of inputs.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">disp(<span style=\"color: #a020f0;\">'parse all options'<\/span>);\r\np.parse([1 2 3 4],2,<span style=\"color: #a020f0;\">'title'<\/span>,<span style=\"color: #a020f0;\">'mytitle'<\/span>);\r\ninputs = p.Results\r\n\r\n<span style=\"color: #228b22;\">% parse without specifying a title<\/span>\r\ndisp(<span style=\"color: #a020f0;\">'parse without specifing title param-value pair'<\/span>);\r\np.parse([1 2 3 4], 3);\r\ninputs = p.Results\r\n\r\n<span style=\"color: #228b22;\">%parse with title, no ntimes<\/span>\r\ndisp(<span style=\"color: #a020f0;\">'parse without optional ntimes'<\/span>);\r\np.parse([5 6 7 8], <span style=\"color: #a020f0;\">'title'<\/span>,<span style=\"color: #a020f0;\">'mytitle'<\/span>);\r\ninputs = p.Results<\/pre>\n<pre style=\"font-style: oblique;\">parse all options\r\n\r\ninputs = \r\n\r\n    mainVector: [1 2 3 4]\r\n        ntimes: 2\r\n         title: 'mytitle'\r\n\r\nparse without specifing title param-value pair\r\n\r\ninputs = \r\n\r\n    mainVector: [1 2 3 4]\r\n        ntimes: 3\r\n         title: 'Default title'\r\n\r\nparse without optional ntimes\r\n\r\ninputs = \r\n\r\n    mainVector: [5 6 7 8]\r\n        ntimes: 1\r\n         title: 'mytitle'<\/pre>\n<p>You can see that the result is a struct with all the inputs broken out into fields. If an optional input was not specified, the default value is placed in the struct.<\/p>\n<p><strong>Use with a function<\/strong><br \/>\nOf course, the purpose of this is easy input validation and organization within a function. Here&#8217;s a sample function and example of validation failure:<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\"><span style=\"color: #0000ff;\">function<\/span> myfun(input1,varargin)\r\np = inputParser;\r\np.addRequired(<span style=\"color: #a020f0;\">'mainVector'<\/span>,@(x) length(x)&gt;1);\r\np.addOptional(<span style=\"color: #a020f0;\">'ntimes'<\/span>,1,@isscalar);\r\np.addParamValue(<span style=\"color: #a020f0;\">'title'<\/span>,<span style=\"color: #a020f0;\">'Default title'<\/span>,@isstr);\r\n\r\np.parse(input1,varargin{:})<\/pre>\n<p>Here we call the function with invalid arguments:<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">myfun([1 2 3],[1 2])<\/pre>\n<pre style=\"font-style: oblique;\">Error using myfun (line 7)\r\nArgument 'ntimes' failed validation isscalar.<\/pre>\n<p>Our validation serves it purpose!<\/p>\n<p>Let us know how if you use <tt>inputParser<\/tt> or another method for checking function input.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever wondered how MATLAB functions make sense of variable function arguments? Many MATLAB functions take multiple optional arguments, or arguments specified as param-value pairs. A&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2012\/02\/13\/parsing-inputs\/\">read more >><\/a><\/p>\n","protected":false},"author":38,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[69],"tags":[195,192,191,190],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/560"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/comments?post=560"}],"version-history":[{"count":20,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/560\/revisions"}],"predecessor-version":[{"id":665,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/560\/revisions\/665"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}