{"id":184,"date":"2009-05-12T19:57:16","date_gmt":"2009-05-12T19:57:16","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2009\/05\/12\/optional-arguments-using-empty-as-placeholder\/"},"modified":"2009-05-06T20:20:51","modified_gmt":"2009-05-06T20:20:51","slug":"optional-arguments-using-empty-as-placeholder","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2009\/05\/12\/optional-arguments-using-empty-as-placeholder\/","title":{"rendered":"Optional Arguments Using Empty as Placeholder"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>I recently posted about <a href=\"https:\/\/blogs.mathworks.com\/loren\/2009\/05\/05\/nice-way-to-set-function-defaults\/\">optional input arguments<\/a> and how to override default values. <a href=\"https:\/\/blogs.mathworks.com\/loren\/2008\/06\/30\/lego-mindstorms-nxt-in-teaching\/\">Gautam<\/a> mentioned wanting to allow empty inputs as defaults.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Function Defaults Using Empty Arrays<\/a><\/li>\r\n         <li><a href=\"#2\">Compare Functions<\/a><\/li>\r\n         <li><a href=\"#3\">Differences<\/a><\/li>\r\n         <li><a href=\"#4\">Any More Thoughts on Defaults?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Function Defaults Using Empty Arrays<a name=\"1\"><\/a><\/h3>\r\n   <p>With a little more thought, I was able to adapt the last program I showed to incorporate empty values taking on the defaults.\r\n       This allows the user to override defaults later in the argument list without having to reset all the previous values.  Let's\r\n      see how this works.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">type <span style=\"color: #A020F0\">somefun2AltEmptyDefs<\/span><\/pre><pre style=\"font-style:oblique\">\r\nfunction y = somefun2AltEmptyDefs(a,b,varargin)\r\n% Some function that requires 2 inputs and has some optional inputs.\r\n\r\n% only want 3 optional inputs at most\r\nnumvarargs = length(varargin);\r\nif numvarargs &gt; 3\r\n    error('myfuns:somefun2Alt:TooManyInputs', ...\r\n        'requires at most 3 optional inputs');\r\nend\r\n\r\n% set defaults for optional inputs\r\noptargs = {eps 17 @magic};\r\n\r\n% skip any new inputs if they are empty\r\nnewVals = cellfun(@(x) ~isempty(x), varargin);\r\n\r\n% now put these defaults into the valuesToUse cell array, \r\n% and overwrite the ones specified in varargin.\r\noptargs(newVals) = varargin(newVals);\r\n% or ...\r\n% [optargs{1:numvarargs}] = varargin{:};\r\n\r\n% Place optional args in memorable variable names\r\n[tol, mynum, func] = optargs{:};\r\n<\/pre><h3>Compare Functions<a name=\"2\"><\/a><\/h3>\r\n   <p>I can use the Compare Against tool (from the Tools menu) to compare my new function with my previous one.  Here's a screenshot.\r\n       You can click on it to see a legible version.\r\n   <\/p>\r\n   <p><html><a href=\"https:\/\/blogs.mathworks.com\/images\/loren\/184\/optargsDiffs.png\"><IMG SRC=\"https:\/\/blogs.mathworks.com\/images\/loren\/184\/optargsDiffsSmall.png\"><\/a><\/html><\/p>\r\n   <h3>Differences<a name=\"3\"><\/a><\/h3>\r\n   <p>There are 2 additional lines and one changed line in the new file.  After setting up the cell array of defaults, I now check\r\n      the <tt>varargin<\/tt> cell array for empty arguments.  There are ones for which I want to retain the default values.  So I find the correct indices\r\n      for them, and only overwrite or add the non-empty values from <tt>varargin<\/tt>.\r\n   <\/p>\r\n   <h3>Any More Thoughts on Defaults?<a name=\"4\"><\/a><\/h3>\r\n   <p>If you have any more thoughts on default values, please post them <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=184#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_f885b9727ce84daca28ff927ca5d2bfd() {\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='f885b9727ce84daca28ff927ca5d2bfd ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' f885b9727ce84daca28ff927ca5d2bfd';\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 2009 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_f885b9727ce84daca28ff927ca5d2bfd()\"><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.8<br><\/p>\r\n<\/div>\r\n<!--\r\nf885b9727ce84daca28ff927ca5d2bfd ##### SOURCE BEGIN #####\r\n%% Optional Arguments Using Empty as Placeholder\r\n% I recently posted about \r\n% <https:\/\/blogs.mathworks.com\/loren\/2009\/05\/05\/nice-way-to-set-function-defaults\/ optional input arguments>\r\n% and how to override default values.\r\n% <https:\/\/blogs.mathworks.com\/loren\/2008\/06\/30\/lego-mindstorms-nxt-in-teaching\/ Gautam>\r\n% mentioned wanting to allow empty inputs as defaults.  \r\n%% Function Defaults Using Empty Arrays\r\n% With a little more\r\n% thought, I was able to adapt the last program I showed to incorporate\r\n% empty values taking on the defaults.  This allows the user to override\r\n% defaults later in the argument list without having to reset all the\r\n% previous values.  Let's see how this works.\r\ntype somefun2AltEmptyDefs\r\n%% Compare Functions\r\n% I can use the Compare Against tool (from the Tools menu) to compare my \r\n% new function with my previous one.  Here's a screenshot.  You can click \r\n% on it to see a legible version.\r\n%\r\n% <html><a href=\"https:\/\/blogs.mathworks.com\/images\/loren\/184\/optargsDiffs.png\"><IMG SRC=\"https:\/\/blogs.mathworks.com\/images\/loren\/184\/optargsDiffsSmall.png\"><\/a><\/html>\r\n%\r\n%% Differences\r\n% There are 2 additional lines and one changed line in the new file.  After\r\n% setting up the cell array of defaults, I now check the |varargin| cell\r\n% array for empty arguments.  There are ones for which I want to retain the\r\n% default values.  So I find the correct indices for them, and only\r\n% overwrite or add the non-empty values from |varargin|.\r\n\r\n%% Any More Thoughts on Defaults?\r\n% If you have any more thoughts on default values, please post them\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=184#respond here>.\r\n\r\n\r\n##### SOURCE END ##### f885b9727ce84daca28ff927ca5d2bfd\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      I recently posted about optional input arguments and how to override default values. Gautam mentioned wanting to allow empty inputs as defaults.\r\n      \r\n   \r\n   Contents\r\n   \r\n   ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2009\/05\/12\/optional-arguments-using-empty-as-placeholder\/\">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,17,30],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/184"}],"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=184"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/184\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}