<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Optional Arguments Using Empty as Placeholder</title>
	<atom:link href="http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/</link>
	<description>Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Thu, 09 Feb 2012 04:19:21 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: zhouyang</title>
		<link>http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/#comment-31792</link>
		<dc:creator>zhouyang</dc:creator>
		<pubDate>Tue, 26 Oct 2010 04:52:11 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/#comment-31792</guid>
		<description>so wonderful!</description>
		<content:encoded><![CDATA[<p>so wonderful!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: K_E_M_O</title>
		<link>http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/#comment-30852</link>
		<dc:creator>K_E_M_O</dc:creator>
		<pubDate>Fri, 27 Nov 2009 00:38:42 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/#comment-30852</guid>
		<description>Thanks a lot.. I am learning how to use matlab and these are very useful for me.</description>
		<content:encoded><![CDATA[<p>Thanks a lot.. I am learning how to use matlab and these are very useful for me.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: João Henriques</title>
		<link>http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/#comment-30330</link>
		<dc:creator>João Henriques</dc:creator>
		<pubDate>Sat, 16 May 2009 23:31:08 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/#comment-30330</guid>
		<description>Sorry, I was away from a MATLAB console when I wrote that :) Minor corrections to the function:

&lt;pre&gt;
function setdefaults(defaults)
    var_names = evalin(&#039;caller&#039;, &#039;who&#039;);
    default_names = fieldnames(defaults);
    must_replace = ~ismember(default_names, var_names);
    for k = find(must_replace)&#039;
        name = default_names{k};
        assignin(&#039;caller&#039;, name, defaults.(name));
    end
end
&lt;/pre&gt;

I&#039;ve been testing it and it&#039;s pretty handy.</description>
		<content:encoded><![CDATA[<p>Sorry, I was away from a MATLAB console when I wrote that :) Minor corrections to the function:</p>
<pre>
function setdefaults(defaults)
    var_names = evalin('caller', 'who');
    default_names = fieldnames(defaults);
    must_replace = ~ismember(default_names, var_names);
    for k = find(must_replace)'
        name = default_names{k};
        assignin('caller', name, defaults.(name));
    end
end
</pre>
<p>I&#8217;ve been testing it and it&#8217;s pretty handy.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: João Henriques</title>
		<link>http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/#comment-30327</link>
		<dc:creator>João Henriques</dc:creator>
		<pubDate>Sat, 16 May 2009 02:48:52 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/#comment-30327</guid>
		<description>Hi! I just wanted to tell you that I found both the last two posts and their comments very insightful. Excellent food for thought! :)

The first method is great, however I&#039;m a bit turned off by the fact that the inputs are all lumped into varargin. As I grew accustomed to Matlab, I learned to identify a function&#039;s arguments with a quick glance at the top line.

I was thinking of accepting the inputs as usual, define a struct with the default values, and pass it to a helper function. The helper function accesses the main function&#039;s variables as defined in the struct, and writes the default values if needed. IMHO it could be an excellent compromise :)

&lt;pre&gt;
function testing()
    something(&#039;testing &#039;, &#039;it&#039;)
    something()
end

function z = something(a, b, c)
    defaults.a = &#039;hey &#039;;
    defaults.b = &#039;you&#039;;
    defaults.c = &#039;!&#039;;
    setdefaults(defaults);
	
    z = [a b c];
end

function setdefaults(defaults)
    var_names = evalin(&#039;caller&#039;, &#039;whos&#039;);
    default_names = fieldnames(defaults);
    must_replace = ~ismember(default_names, var_names);
    for k = find(must_replace)
        name = default_names(k);
        assignin(&#039;caller&#039;, name, defaults.(name));
    end
end
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Hi! I just wanted to tell you that I found both the last two posts and their comments very insightful. Excellent food for thought! :)</p>
<p>The first method is great, however I&#8217;m a bit turned off by the fact that the inputs are all lumped into varargin. As I grew accustomed to Matlab, I learned to identify a function&#8217;s arguments with a quick glance at the top line.</p>
<p>I was thinking of accepting the inputs as usual, define a struct with the default values, and pass it to a helper function. The helper function accesses the main function&#8217;s variables as defined in the struct, and writes the default values if needed. IMHO it could be an excellent compromise :)</p>
<pre>
function testing()
    something('testing ', 'it')
    something()
end

function z = something(a, b, c)
    defaults.a = 'hey ';
    defaults.b = 'you';
    defaults.c = '!';
    setdefaults(defaults);

    z = [a b c];
end

function setdefaults(defaults)
    var_names = evalin('caller', 'whos');
    default_names = fieldnames(defaults);
    must_replace = ~ismember(default_names, var_names);
    for k = find(must_replace)
        name = default_names(k);
        assignin('caller', name, defaults.(name));
    end
end
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/#comment-30321</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 15 May 2009 12:38:34 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/#comment-30321</guid>
		<description>Hi Jessee-

Unfortunately I don&#039;t know of a way to do that right now.  Having syntax-highlighted code is on our wish list too.

--Loren</description>
		<content:encoded><![CDATA[<p>Hi Jessee-</p>
<p>Unfortunately I don&#8217;t know of a way to do that right now.  Having syntax-highlighted code is on our wish list too.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jessee</title>
		<link>http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/#comment-30320</link>
		<dc:creator>Jessee</dc:creator>
		<pubDate>Fri, 15 May 2009 12:22:56 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/#comment-30320</guid>
		<description>Hi Loren,
I&#039;ve been following your blog for several years now and it&#039;s been interesting to see how the format of the website, style-sheets, etc. have changed.  
I&#039;ve noticed lately when you use the &quot;type&quot; command to display the text of a function, it prints it in hard to read italics.  I&#039;ve found that when I&#039;m reading code, it&#039;s easier to read and understand if it uses the Matlab Editor&#039;s standard syntax highlighting.  Is there a way (maybe using CSS) to have your blog posts format anything between &quot;pre&quot; tags the same way the Editor does it?
-Jessee</description>
		<content:encoded><![CDATA[<p>Hi Loren,<br />
I&#8217;ve been following your blog for several years now and it&#8217;s been interesting to see how the format of the website, style-sheets, etc. have changed.<br />
I&#8217;ve noticed lately when you use the &#8220;type&#8221; command to display the text of a function, it prints it in hard to read italics.  I&#8217;ve found that when I&#8217;m reading code, it&#8217;s easier to read and understand if it uses the Matlab Editor&#8217;s standard syntax highlighting.  Is there a way (maybe using CSS) to have your blog posts format anything between &#8220;pre&#8221; tags the same way the Editor does it?<br />
-Jessee</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Petr</title>
		<link>http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/#comment-30312</link>
		<dc:creator>Petr</dc:creator>
		<pubDate>Wed, 13 May 2009 19:00:07 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/#comment-30312</guid>
		<description>Loren

I just posted the feature request as suggested.

I realized that I did not provide the way I currently consider the most suitable for me. I have converged to an approach very similar to the one in Ben&#039;s comments of the previous article, i.e. using key-value pairs, with defaults defined in a structure in the beginning of a function and then overwriting them with the respective fields of parameters structure. 

The only difference is that I prefer to allow for key-value pairs given in the same way as in many MATLAB graphics commands (i.e. converting the varargin cell array to structure inside the function), rather then directly expecting a structure as an argument.

Thanks for showing all these possibilities.</description>
		<content:encoded><![CDATA[<p>Loren</p>
<p>I just posted the feature request as suggested.</p>
<p>I realized that I did not provide the way I currently consider the most suitable for me. I have converged to an approach very similar to the one in Ben&#8217;s comments of the previous article, i.e. using key-value pairs, with defaults defined in a structure in the beginning of a function and then overwriting them with the respective fields of parameters structure. </p>
<p>The only difference is that I prefer to allow for key-value pairs given in the same way as in many MATLAB graphics commands (i.e. converting the varargin cell array to structure inside the function), rather then directly expecting a structure as an argument.</p>
<p>Thanks for showing all these possibilities.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/#comment-30311</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Wed, 13 May 2009 15:48:25 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/#comment-30311</guid>
		<description>Petr-

I believe R does something similar as well.  Can you please put this into our system as an enhancement request &lt;a href=&quot;http://www.mathworks.com/support/service_requests/contact_support.do&quot; rel=&quot;nofollow&quot;&gt;here&lt;/a&gt;?  It would be more meaningful coming directly from a customer.  Thanks.

--Loren</description>
		<content:encoded><![CDATA[<p>Petr-</p>
<p>I believe R does something similar as well.  Can you please put this into our system as an enhancement request <a href="http://www.mathworks.com/support/service_requests/contact_support.do" rel="nofollow">here</a>?  It would be more meaningful coming directly from a customer.  Thanks.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Petr</title>
		<link>http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/#comment-30310</link>
		<dc:creator>Petr</dc:creator>
		<pubDate>Wed, 13 May 2009 15:42:08 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/#comment-30310</guid>
		<description>Hi Loren.

I have been trying to find the optimal way of dealing with optional arguments and default values for several years, but I find myself of not being able to stick with one best approach. I am trying new ways over and over.

I must actually say, that I like the Python way of dealing with optional arguments and default values, i.e. specifying them directly in the function header. It would be nice to have this feature in MATLAB as well:

function foo(reqArg, optArg1=12, optArg2=&#039;whatever&#039;)

Petr</description>
		<content:encoded><![CDATA[<p>Hi Loren.</p>
<p>I have been trying to find the optimal way of dealing with optional arguments and default values for several years, but I find myself of not being able to stick with one best approach. I am trying new ways over and over.</p>
<p>I must actually say, that I like the Python way of dealing with optional arguments and default values, i.e. specifying them directly in the function header. It would be nice to have this feature in MATLAB as well:</p>
<p>function foo(reqArg, optArg1=12, optArg2=&#8217;whatever&#8217;)</p>
<p>Petr</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/#comment-30309</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Wed, 13 May 2009 11:00:19 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/#comment-30309</guid>
		<description>Dan-

I could definitely use nargchk but mentally what I am doing is simple enough that I don&#039;t think to.  It doesn&#039;t help with setting the defaults either.

--Loren</description>
		<content:encoded><![CDATA[<p>Dan-</p>
<p>I could definitely use nargchk but mentally what I am doing is simple enough that I don&#8217;t think to.  It doesn&#8217;t help with setting the defaults either.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
</channel>
</rss>

