<?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: Optional Arguments Using Empty as Placeholder</title>
	<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 &#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>Mon, 23 Nov 2009 00:28:25 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
		<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>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('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
&lt;/pre&gt;

I've been testing it and it'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>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'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'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'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('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
&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>http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/#comment-30321</guid>
		<description>Hi Jessee-

Unfortunately I don'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>http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/#comment-30320</guid>
		<description>Hi Loren,
I've been following your blog for several years now and it's been interesting to see how the format of the website, style-sheets, etc. have changed.  
I've noticed lately when you use the "type" command to display the text of a function, it prints it in hard to read italics.  I've found that when I'm reading code, it's easier to read and understand if it uses the Matlab Editor's standard syntax highlighting.  Is there a way (maybe using CSS) to have your blog posts format anything between "pre" 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>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'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>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="http://www.mathworks.com/support/service_requests/contact_support.do" rel="nofollow"&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>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='whatever')

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>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't think to.  It doesn'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>
	<item>
		<title>By: Dan</title>
		<link>http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/#comment-30308</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Wed, 13 May 2009 07:25:26 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2009/05/12/optional-arguments-using-empty-as-placeholder/#comment-30308</guid>
		<description>The past couple of posts have been really helpful to me for dealing with setting input arguments. I am surprised by how the number of optional arguments is being checked. Is there a reason that you did not use the nargchk function?</description>
		<content:encoded><![CDATA[<p>The past couple of posts have been really helpful to me for dealing with setting input arguments. I am surprised by how the number of optional arguments is being checked. Is there a reason that you did not use the nargchk function?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
