<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Nice Way to Set Function Defaults</title>
	<link>http://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/</link>
	<description>Loren Shure  works on design of the MATLAB language at &#60;a href="http://www.mathworks.com/"&#62;The MathWorks&#60;/a&#62;. She writes here about once a week on MATLAB programming and related topics. &#60;br&#62;&#60;br&#62;&#60;a href="/images/loren-full.jpg"&#62;&#60;img src="/images/loren.jpg"&#62;&#60;/a&#62;</description>
	<pubDate>Sun, 22 Nov 2009 23:22:56 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
		<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/#comment-30651</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Tue, 29 Sep 2009 18:51:50 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/#comment-30651</guid>
		<description>Hi David-

Not that I know of.  You might use the link on the right to request an enhancement with technical support.

--Loren</description>
		<content:encoded><![CDATA[<p>Hi David-</p>
<p>Not that I know of.  You might use the link on the right to request an enhancement with technical support.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David</title>
		<link>http://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/#comment-30650</link>
		<dc:creator>David</dc:creator>
		<pubDate>Tue, 29 Sep 2009 18:50:14 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/#comment-30650</guid>
		<description>Hi Loren,
  I discovered inputParser a few months ago and am delighted.  However, I'd like to be able to change some of the values of passed arguments within the function as sometimes the default values of input parameters depends on the value of other input paramters.  For example, in the function below, the default size of the moving average window depends on the size of the data being smoothed.  To do this, I create a new variable called "wind_size." However, I'd rather modify the value of p.Results.wind_size so that I have less variables to keep track of. When I try to do this however, I get the following error message:
"??? Setting the 'Results' property of the 'inputParser' class is not allowed."

Is there a way to get around that limitation?
    much thanks,
        -David


&lt;pre&gt;
function outdata=movavg(indata,varargin)
%Smooths indata with a boxcar windowed moving average

p=inputParser;

p.addRequired('indata',@isnumeric);
p.addParamValue('wind_size',[],@isnumeric);

p.parse(indata,varargin{:});

if isempty(p.Results.wind_size)
    wind_size=round(length(indata)/20);
else
    wind_size=p.Results.wind_size;
end

if wind_size&#60;1,
   wind_size=1; 
end

n_outdata=length(indata)-wind_size+1;
outdata=zeros(1,n_outdata);
for a=1:n_outdata,
    outdata(a)=mean(indata(a:a+wind_size-1));
end
    
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Hi Loren,<br />
  I discovered inputParser a few months ago and am delighted.  However, I&#8217;d like to be able to change some of the values of passed arguments within the function as sometimes the default values of input parameters depends on the value of other input paramters.  For example, in the function below, the default size of the moving average window depends on the size of the data being smoothed.  To do this, I create a new variable called &#8220;wind_size.&#8221; However, I&#8217;d rather modify the value of p.Results.wind_size so that I have less variables to keep track of. When I try to do this however, I get the following error message:<br />
&#8220;??? Setting the &#8216;Results&#8217; property of the &#8216;inputParser&#8217; class is not allowed.&#8221;</p>
<p>Is there a way to get around that limitation?<br />
    much thanks,<br />
        -David</p>
<pre>
function outdata=movavg(indata,varargin)
%Smooths indata with a boxcar windowed moving average

p=inputParser;

p.addRequired('indata',@isnumeric);
p.addParamValue('wind_size',[],@isnumeric);

p.parse(indata,varargin{:});

if isempty(p.Results.wind_size)
    wind_size=round(length(indata)/20);
else
    wind_size=p.Results.wind_size;
end

if wind_size&lt;1,
   wind_size=1;
end

n_outdata=length(indata)-wind_size+1;
outdata=zeros(1,n_outdata);
for a=1:n_outdata,
    outdata(a)=mean(indata(a:a+wind_size-1));
end
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt</title>
		<link>http://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/#comment-30522</link>
		<dc:creator>Matt</dc:creator>
		<pubDate>Mon, 10 Aug 2009 17:26:44 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/#comment-30522</guid>
		<description>Loren,

Thanks for the write-up.  In my opinion, inputting/outputting variables to a function is the bane of coding in any language.  Everyone has their own opinion on how it should be done, and none of them work all that well apart, let alone together when you integrate code.  This is an especially a large problem when using a development coding environment like MATLAB as not much is ever polished when it gets used.

I am pleased to see the &lt;a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/inputparser.html" rel="nofollow"&gt;inputParser&lt;/a&gt; function added to the default framework though.  Great step in the right direction for elegant and functional parsing of an input stream!  As a user of the &lt;a href="http://www.python.org/" rel="nofollow"&gt;Python&lt;/a&gt; module &lt;a href="http://pyparsing.wikispaces.com/" rel="nofollow"&gt;pyparsing&lt;/a&gt;, I have seen first hand how creating a scheme for your parser can pay dividends on code flexibility, simplicity, and readability.

I suggest you revisit this same exercise using &lt;a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/inputparser.html" rel="nofollow"&gt;inputParser&lt;/a&gt; to put the virtues on the 'big stage'.

-Matt</description>
		<content:encoded><![CDATA[<p>Loren,</p>
<p>Thanks for the write-up.  In my opinion, inputting/outputting variables to a function is the bane of coding in any language.  Everyone has their own opinion on how it should be done, and none of them work all that well apart, let alone together when you integrate code.  This is an especially a large problem when using a development coding environment like MATLAB as not much is ever polished when it gets used.</p>
<p>I am pleased to see the <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/inputparser.html" rel="nofollow">inputParser</a> function added to the default framework though.  Great step in the right direction for elegant and functional parsing of an input stream!  As a user of the <a href="http://www.python.org/" rel="nofollow">Python</a> module <a href="http://pyparsing.wikispaces.com/" rel="nofollow">pyparsing</a>, I have seen first hand how creating a scheme for your parser can pay dividends on code flexibility, simplicity, and readability.</p>
<p>I suggest you revisit this same exercise using <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/inputparser.html" rel="nofollow">inputParser</a> to put the virtues on the &#8216;big stage&#8217;.</p>
<p>-Matt</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bryan Reed</title>
		<link>http://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/#comment-30472</link>
		<dc:creator>Bryan Reed</dc:creator>
		<pubDate>Thu, 16 Jul 2009 20:47:46 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/#comment-30472</guid>
		<description>I was about to post my method, but I saw that Gautam Vallabha already posted it, absolutely verbatim!  The code fragment "~exist('varname','var') &#124;&#124; isempty(varname)" appears all over the place in functions I've written over the past few years.  I find this method to be extremely concise, readable, and flexible.  You can change the order or number of parameters in the function definition and it doesn't change the code for setting defaults.  I don't know that it has any downsides, apart from the case where you need varargin because the user can supply an unlimited number of options.

Typically I'll only use varargin for handing options down to a lower-level built-in function, for example if the function I'm writing is basically a wrapper for MATLAB's plot() function.  Then the user can specify line types and colors etc., and I as the programmer don't have to think about how that would work.</description>
		<content:encoded><![CDATA[<p>I was about to post my method, but I saw that Gautam Vallabha already posted it, absolutely verbatim!  The code fragment &#8220;~exist(&#8217;varname&#8217;,'var&#8217;) || isempty(varname)&#8221; appears all over the place in functions I&#8217;ve written over the past few years.  I find this method to be extremely concise, readable, and flexible.  You can change the order or number of parameters in the function definition and it doesn&#8217;t change the code for setting defaults.  I don&#8217;t know that it has any downsides, apart from the case where you need varargin because the user can supply an unlimited number of options.</p>
<p>Typically I&#8217;ll only use varargin for handing options down to a lower-level built-in function, for example if the function I&#8217;m writing is basically a wrapper for MATLAB&#8217;s plot() function.  Then the user can specify line types and colors etc., and I as the programmer don&#8217;t have to think about how that would work.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/#comment-30315</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Thu, 14 May 2009 10:56:27 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/#comment-30315</guid>
		<description>Kent-

I think I misunderstood your code.  I thought it depended on names being the same.  Clearly not true.

--Loren</description>
		<content:encoded><![CDATA[<p>Kent-</p>
<p>I think I misunderstood your code.  I thought it depended on names being the same.  Clearly not true.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kent Conover</title>
		<link>http://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/#comment-30313</link>
		<dc:creator>Kent Conover</dc:creator>
		<pubDate>Wed, 13 May 2009 21:58:05 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/#comment-30313</guid>
		<description>Hi Loren,

Thanks for replying to my post:

#  Loren replied on May 5th, 2009 at 20:17 UTC :

Kent- Your code seems to depend on naming the variables in the caller the same as the names in the actual function definition. Seems a bit fragile for my taste.

–Loren

However, I am unsure about your concern. Perhaps my example was unclear. But to address your point, I have no problem calling such functions with different variable names in the caller function.

Say func1 is defined thus:

function x = func1(y)

if isundefined('y')
  y = 1;
end

x = 2*y;

Then func1 can be called like this:
&#62;&#62;z = 2;
&#62;&#62;func1(z)
ans = 
  2

Moreover, this function works even if z is empty or absent.

ie:

&#62;&#62;clear z
&#62;&#62;func1(z)
ans = 
  2

or 

&#62;&#62;func1()
ans = 
  2

So, I can't see your point about fragility, unless you are concerned about maintaining the order and position of the parameters passed - in which case you have me!. I keep the number of passed parameters &#60;= 6. Nevertheless, I value your perspective, and I would appreciate knowing any further thoughts that you have on this issue.

Cheers!

-Kent</description>
		<content:encoded><![CDATA[<p>Hi Loren,</p>
<p>Thanks for replying to my post:</p>
<p>#  Loren replied on May 5th, 2009 at 20:17 UTC :</p>
<p>Kent- Your code seems to depend on naming the variables in the caller the same as the names in the actual function definition. Seems a bit fragile for my taste.</p>
<p>–Loren</p>
<p>However, I am unsure about your concern. Perhaps my example was unclear. But to address your point, I have no problem calling such functions with different variable names in the caller function.</p>
<p>Say func1 is defined thus:</p>
<p>function x = func1(y)</p>
<p>if isundefined(&#8217;y')<br />
  y = 1;<br />
end</p>
<p>x = 2*y;</p>
<p>Then func1 can be called like this:<br />
&gt;&gt;z = 2;<br />
&gt;&gt;func1(z)<br />
ans =<br />
  2</p>
<p>Moreover, this function works even if z is empty or absent.</p>
<p>ie:</p>
<p>&gt;&gt;clear z<br />
&gt;&gt;func1(z)<br />
ans =<br />
  2</p>
<p>or </p>
<p>&gt;&gt;func1()<br />
ans =<br />
  2</p>
<p>So, I can&#8217;t see your point about fragility, unless you are concerned about maintaining the order and position of the parameters passed - in which case you have me!. I keep the number of passed parameters &lt;= 6. Nevertheless, I value your perspective, and I would appreciate knowing any further thoughts that you have on this issue.</p>
<p>Cheers!</p>
<p>-Kent</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Omid</title>
		<link>http://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/#comment-30306</link>
		<dc:creator>Omid</dc:creator>
		<pubDate>Tue, 12 May 2009 18:06:47 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/#comment-30306</guid>
		<description>Yair-

What can I say! That was indeed "counter-intuitive (and undocumented) but true." I applied a generally true statement to something that turned out to be a special case! Thanks for pointing this out.
I tested this in R2008a and R2008b. The difference is actually an order of magnitude!

R2008a

&#62;&#62; c = mat2cell(1:1e6,1,repmat(1,1,1e6));
&#62;&#62; tic, d=cellfun('isempty',c); toc
Elapsed time is 0.024467 seconds.
&#62;&#62; tic, d=cellfun(@isempty,c); toc
Elapsed time is 0.929305 seconds.


R2008b

&#62;&#62; c = mat2cell(1:1e6,1,repmat(1,1,1e6));
&#62;&#62; tic, d=cellfun('isempty',c); toc
Elapsed time is 0.014660 seconds.
&#62;&#62; tic, d=cellfun(@isempty,c); toc
Elapsed time is 0.557050 seconds.


(These timings are after some warm up - I didn't use Steve Edins' timeit so that the results can be directly compared with what you reported for R2007b.)

-Omid

PS. Yair and Martin- 
You've both written my name as Omar, which is strange given that the last two letters are totally different!</description>
		<content:encoded><![CDATA[<p>Yair-</p>
<p>What can I say! That was indeed &#8220;counter-intuitive (and undocumented) but true.&#8221; I applied a generally true statement to something that turned out to be a special case! Thanks for pointing this out.<br />
I tested this in R2008a and R2008b. The difference is actually an order of magnitude!</p>
<p>R2008a</p>
<p>&gt;&gt; c = mat2cell(1:1e6,1,repmat(1,1,1e6));<br />
&gt;&gt; tic, d=cellfun(&#8217;isempty&#8217;,c); toc<br />
Elapsed time is 0.024467 seconds.<br />
&gt;&gt; tic, d=cellfun(@isempty,c); toc<br />
Elapsed time is 0.929305 seconds.</p>
<p>R2008b</p>
<p>&gt;&gt; c = mat2cell(1:1e6,1,repmat(1,1,1e6));<br />
&gt;&gt; tic, d=cellfun(&#8217;isempty&#8217;,c); toc<br />
Elapsed time is 0.014660 seconds.<br />
&gt;&gt; tic, d=cellfun(@isempty,c); toc<br />
Elapsed time is 0.557050 seconds.</p>
<p>(These timings are after some warm up - I didn&#8217;t use Steve Edins&#8217; timeit so that the results can be directly compared with what you reported for R2007b.)</p>
<p>-Omid</p>
<p>PS. Yair and Martin-<br />
You&#8217;ve both written my name as Omar, which is strange given that the last two letters are totally different!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/#comment-30304</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Mon, 11 May 2009 18:45:51 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/#comment-30304</guid>
		<description>Yair-

We could improve cellfun to check function handles to see if they match specified strings.  Even then MATLAB would have to be careful in case the user has overridden the built-in version of whatever the string points to.

--Loren</description>
		<content:encoded><![CDATA[<p>Yair-</p>
<p>We could improve cellfun to check function handles to see if they match specified strings.  Even then MATLAB would have to be careful in case the user has overridden the built-in version of whatever the string points to.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yair Altman</title>
		<link>http://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/#comment-30303</link>
		<dc:creator>Yair Altman</dc:creator>
		<pubDate>Mon, 11 May 2009 18:43:34 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/#comment-30303</guid>
		<description>Omar &#38; Martin - cellfun(@isempty,...) may be better for the compiler, but it is actually *Worse* than cellfun('isempty') for performance. This is indeed counter-intuitive (and undocumented) but true. The reason appears to be that using 'isempty' (as well as the other predefined string functions ('isreal', 'islogical', 'length', 'ndims', 'prodofsize') uses specific code-branches optimized for performance (at least as of R2007b - I haven't yet tested this on newer releases):

&lt;code&gt;
&#62;&#62; c = mat2cell(1:1e6,1,repmat(1,1,1e6));
&#62;&#62; tic, d=cellfun('isempty',c); toc
Elapsed time is 0.115583 seconds.
&#62;&#62; tic, d=cellfun(@isempty,c); toc
Elapsed time is 7.493989 seconds.
&lt;/code&gt;

Now, the obvious solution would be for the internal Matlab code to check for function-handle equality and use the optimized version if possible. Perhaps a future Matlab release will do this. Hint hint MTW? :-)

Yair Altman</description>
		<content:encoded><![CDATA[<p>Omar &amp; Martin - cellfun(@isempty,&#8230;) may be better for the compiler, but it is actually *Worse* than cellfun(&#8217;isempty&#8217;) for performance. This is indeed counter-intuitive (and undocumented) but true. The reason appears to be that using &#8216;isempty&#8217; (as well as the other predefined string functions (&#8217;isreal&#8217;, &#8216;islogical&#8217;, &#8216;length&#8217;, &#8216;ndims&#8217;, &#8216;prodofsize&#8217;) uses specific code-branches optimized for performance (at least as of R2007b - I haven&#8217;t yet tested this on newer releases):</p>
<p><code><br />
&gt;&gt; c = mat2cell(1:1e6,1,repmat(1,1,1e6));<br />
&gt;&gt; tic, d=cellfun(&#8217;isempty&#8217;,c); toc<br />
Elapsed time is 0.115583 seconds.<br />
&gt;&gt; tic, d=cellfun(@isempty,c); toc<br />
Elapsed time is 7.493989 seconds.<br />
</code></p>
<p>Now, the obvious solution would be for the internal Matlab code to check for function-handle equality and use the optimized version if possible. Perhaps a future Matlab release will do this. Hint hint MTW? :-)</p>
<p>Yair Altman</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Martin</title>
		<link>http://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/#comment-30300</link>
		<dc:creator>Martin</dc:creator>
		<pubDate>Mon, 11 May 2009 10:26:22 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/#comment-30300</guid>
		<description>Omar,

I agree about the logical indexing. I was not aware of the issues with the function handle, so thanks for pointing that out!</description>
		<content:encoded><![CDATA[<p>Omar,</p>
<p>I agree about the logical indexing. I was not aware of the issues with the function handle, so thanks for pointing that out!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
