{"id":2548,"date":"2010-04-02T12:57:41","date_gmt":"2010-04-02T12:57:41","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2010\/04\/02\/setting-default-values\/"},"modified":"2010-04-02T12:57:41","modified_gmt":"2010-04-02T12:57:41","slug":"setting-default-values","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2010\/04\/02\/setting-default-values\/","title":{"rendered":"Setting Default Values"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction><\/introduction>\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911\">Brett<\/a>'s Pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27056-set-default-values\">SetDefaultValue<\/a>, by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/22427\">Richie Cotton<\/a>.\r\n   <\/p>\r\n   <p>Richie wrote in the File Exchange introduction to his SetDefaultValue submission that \"it's so simple that I nearly didn't\r\n      upload this, but it does make your functions cleaner.\" In fact, it <i>is<\/i> extremely simple, but it is also widely useful. Anyone who has ever written a program, function, or algorithm--in MATLAB\r\n      or in any other language--has likely had to deal with variable initializations umpteen times. Many of us have come up with\r\n      clever ways of simplifying the process or, as Richie might say, \"prettifying\" the code.\r\n   <\/p>\r\n   <p><tt>SetDefaultValue<\/tt> cleverly uses simple <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/evalin.html\"><tt>evalin<\/tt><\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/assignin.html\"><tt>assignin<\/tt><\/a> calls to streamline and prettify the process.\r\n   <\/p>\r\n   <p>For instance, to conditionally set <tt>x<\/tt> to 1 and <tt>y<\/tt> to 3 as defaults, one might write:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">if<\/span> nargin &lt; 1 || isempty(x)\r\n   x = 1;\r\n<span style=\"color: #0000FF\">end<\/span>\r\n\r\n<span style=\"color: #0000FF\">if<\/span> nargin &lt; 2 || isempty(y)\r\n   y = 3;\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">\r\n<\/pre><p>Richie's submission introduction referred to <a href=\"https:\/\/blogs.mathworks.com\/loren\/2009\/05\/05\/nice-way-to-set-function-defaults\/\">Loren's blog about setting defaults<\/a>. Loren described the use of a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/switch.html\"><tt>switch\/case<\/tt><\/a> approach, switching based on the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/nargout.html\">number of input arguments.<\/a> That is a perfectly reasonable approach, and one that I've used many times.\r\n   <\/p>\r\n   <p>My personal style usually has me writing something along these lines:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% FUNCTION SYNTAX: foo(reqdArg1,reqdArg2,varargin)<\/span>\r\n\r\n<span style=\"color: #228B22\">% Defaults<\/span>\r\nx = 1;y = 2;\r\n<span style=\"color: #0000FF\">if<\/span> nargin &gt; 2, x = varargin{1};<span style=\"color: #0000FF\">end<\/span>\r\n<span style=\"color: #0000FF\">if<\/span> nargin &gt; 3, y = varargin{2};<span style=\"color: #0000FF\">end<\/span><\/pre><p>With Richie's function, one could replace those lines with these:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">SetDefaultValue(1, <span style=\"color: #A020F0\">'x'<\/span>, 1);\r\nSetDefaultValue(2, <span style=\"color: #A020F0\">'y'<\/span>, 3);<\/pre><p>Sometimes, the simplest functions are the most useful ones!<\/p>\r\n   <p>Anyone else have any clever ways to share for dealing with default values? Share them, or your thoughts on the approaches\r\n      mentioned here, using the <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2548#respond\">comments<\/a> box below.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_91f817eb72ec44b180d51ffb2eb90896() {\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='91f817eb72ec44b180d51ffb2eb90896 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 91f817eb72ec44b180d51ffb2eb90896';\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 = 'Brett Shoelson';\r\n        copyright = 'Copyright 2010 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_91f817eb72ec44b180d51ffb2eb90896()\"><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.10<br><\/p>\r\n<\/div>\r\n<!--\r\n91f817eb72ec44b180d51ffb2eb90896 ##### SOURCE BEGIN #####\r\n%% Setting Default Values\r\n%% \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911 Brett>'s Pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27056-set-default-values SetDefaultValue>, by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/22427 Richie Cotton>.\r\n%% \r\n% Richie wrote in the File Exchange introduction to his SetDefaultValue\r\n% submission that \"it's so simple that I nearly didn't upload this, but it\r\n% does make your functions cleaner.\" In fact, it _is_ extremely simple, but\r\n% it is also widely useful. Anyone who has ever written a program,\r\n% function, or algorithmREPLACE_WITH_DASH_DASHin MATLAB or in any other languageREPLACE_WITH_DASH_DASHhas likely\r\n% had to deal with variable initializations umpteen times. Many of us have\r\n% come up with clever ways of simplifying the process or, as Richie might\r\n% say, \"prettifying\" the code.\r\n\r\n%%\r\n% |SetDefaultValue| cleverly uses simple\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/evalin.html\r\n% |evalin|> and\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/assignin.html |assignin|> \r\n% calls to streamline and prettify the process.\r\n\r\n%% \r\n% For instance, to conditionally set |x| to 1 and |y| to 3 as defaults, one might write:\r\n%%\r\nif nargin < 1 || isempty(x) \r\n   x = 1; \r\nend\r\n\r\nif nargin < 2 || isempty(y) \r\n   y = 3; \r\nend \r\n\r\n%%\r\n% Richie's submission introduction referred to\r\n% <https:\/\/blogs.mathworks.com\/loren\/2009\/05\/05\/nice-way-to-set-function-defaults\/ Loren's blog about setting defaults>. Loren described the use of a\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/switch.html |switch\/case|> approach, switching based on the\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/nargout.html number of input arguments.> That \r\n% is a perfectly reasonable approach, and one that I've used many times.\r\n\r\n%%\r\n% My personal style usually has me writing something along these lines:\r\n\r\n% FUNCTION SYNTAX: foo(reqdArg1,reqdArg2,varargin)\r\n\r\n% Defaults\r\nx = 1;y = 2;\r\nif nargin > 2, x = varargin{1};end\r\nif nargin > 3, y = varargin{2};end\r\n\r\n%%\r\n% With Richie's function, one could replace those lines with these:\r\n\r\nSetDefaultValue(1, 'x', 1);\r\nSetDefaultValue(2, 'y', 3);\r\n\r\n%%\r\n% Sometimes, the simplest functions are the most useful ones!\r\n\r\n%% \r\n% Anyone else have any clever ways to share for dealing with default\r\n% values? Share them, or your thoughts on the approaches mentioned here,\r\n% using the <https:\/\/blogs.mathworks.com\/pick\/?p=2548#respond comments> box\r\n% below.\r\n##### SOURCE END ##### 91f817eb72ec44b180d51ffb2eb90896\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n   Brett's Pick this week is SetDefaultValue, by Richie Cotton.\r\n   \r\n   Richie wrote in the File Exchange introduction to his SetDefaultValue submission that \"it's so simple that I nearly... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2010\/04\/02\/setting-default-values\/\">read more >><\/a><\/p>","protected":false},"author":34,"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\/2548"}],"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\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2548"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2548\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}