<?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: Using str2func for Anonymous Function Handle Creation</title>
	<link>http://blogs.mathworks.com/loren/2009/03/13/using-str2func-for-anonymous-function-handle-creation/</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 01:03:06 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
		<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2009/03/13/using-str2func-for-anonymous-function-handle-creation/#comment-30823</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Thu, 19 Nov 2009 21:10:01 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2009/03/13/using-str2func-for-anonymous-function-handle-creation/#comment-30823</guid>
		<description>Yair-

Sorry for the oversight - definitely not intentional.  Please use the link on the right to make an enhancement request via technical support.  I don't think it is entirely compatible since in a regular function, the function has its return args listed in the program.  And now return would depend on position.  But would it obey nargout?  And what if the results returned were different for different values of nargout?  Clearly, I haven't thought about this enough to evaluate if it's even realistic.

In your example with anonymous function, you can get the same result now with

&lt;pre class="code"&gt;

myfunc = @(x) (x==1)*2 +(x==2)*3

&gt;&gt; 
myfunc = @(x) (x==1)*2 +(x==2)*3
myfunc = 
    @(x)(x==1)*2+(x==2)*3
&gt;&gt; myfunc([1 3 2])
ans =
     2     0     3
&lt;/pre&gt;


--Loren</description>
		<content:encoded><![CDATA[<p>Yair-</p>
<p>Sorry for the oversight - definitely not intentional.  Please use the link on the right to make an enhancement request via technical support.  I don&#8217;t think it is entirely compatible since in a regular function, the function has its return args listed in the program.  And now return would depend on position.  But would it obey nargout?  And what if the results returned were different for different values of nargout?  Clearly, I haven&#8217;t thought about this enough to evaluate if it&#8217;s even realistic.</p>
<p>In your example with anonymous function, you can get the same result now with</p>
<pre class="code">

myfunc = @(x) (x==1)*2 +(x==2)*3

>>
myfunc = @(x) (x==1)*2 +(x==2)*3
myfunc =
    @(x)(x==1)*2+(x==2)*3
>> myfunc([1 3 2])
ans =
     2     0     3
</pre>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yair Altman</title>
		<link>http://blogs.mathworks.com/loren/2009/03/13/using-str2func-for-anonymous-function-handle-creation/#comment-30822</link>
		<dc:creator>Yair Altman</dc:creator>
		<pubDate>Thu, 19 Nov 2009 20:02:08 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2009/03/13/using-str2func-for-anonymous-function-handle-creation/#comment-30822</guid>
		<description>hi again Loren - I think my small question (#10 above, http://blogs.mathworks.com/loren/2009/03/13/using-str2func-for-anonymous-function-handle-creation/#comment-30797 ) may have gotten lost in the Oyster/Steve discussion. Could you please revisit?

Suggestion: perhaps enable return to accept arguments:

myfunc = @(x) (if (x==1) return(2); else return(3); end);

This would seem to be entirely backward compatible with existing code and may be useful for Matlab functions in general (not just anonymous):

function [a,b] = myfunc(data)
  % do some useful computation
  return (result1,result2);
end

Thanks,
Yair</description>
		<content:encoded><![CDATA[<p>hi again Loren - I think my small question (#10 above, <a href="http://blogs.mathworks.com/loren/2009/03/13/using-str2func-for-anonymous-function-handle-creation/#comment-30797" rel="nofollow">http://blogs.mathworks.com/loren/2009/03/13/using-str2func-for-anonymous-function-handle-creation/#comment-30797</a> ) may have gotten lost in the Oyster/Steve discussion. Could you please revisit?</p>
<p>Suggestion: perhaps enable return to accept arguments:</p>
<p>myfunc = @(x) (if (x==1) return(2); else return(3); end);</p>
<p>This would seem to be entirely backward compatible with existing code and may be useful for Matlab functions in general (not just anonymous):</p>
<p>function [a,b] = myfunc(data)<br />
  % do some useful computation<br />
  return (result1,result2);<br />
end</p>
<p>Thanks,<br />
Yair</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jiro</title>
		<link>http://blogs.mathworks.com/loren/2009/03/13/using-str2func-for-anonymous-function-handle-creation/#comment-30811</link>
		<dc:creator>Jiro</dc:creator>
		<pubDate>Wed, 18 Nov 2009 03:23:44 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2009/03/13/using-str2func-for-anonymous-function-handle-creation/#comment-30811</guid>
		<description>OysterEngineer,

I second Loren's suggestion on giving anonymous and nested functions a try. It took me a little while to get used to the new syntax for anonymous function, but just like any other MATLAB syntax, I am completely used to it now. I had VERY little programming experience when I started using MATLAB (I may have programmed a little in BASIC), so the learning curve was steep, but after that, MATLAB code was MATLAB code, including anonymous functions.

Another relevant example is the new Object Oriented Programming capability that was introduced in R2008a. It's a completely new programming construct, which may seem extremely foreign to a traditional MATLAB user. I was one of those people. I had no experience in object oriented programming, so I was very confused. I think it took me about a year to get my head around it. Now, it feels very natural, and I love it. It changed how I code in MATLAB, and I can create more sophisticated applications with it.

I think we should not dismiss a particular feature just because it is a foreign concept or syntax. Although, I do agree that there should be added benefits for any new programming constructs. I hope that some of the discussions in this post, as well as the other posts by Loren, will convince you of the benefits of anonymous functions. I do recommend giving them a try.

p.s. BTW, I find Steve's example with the anonymous function so much easier to understand, because I know how to interpret that syntax. This is how you would read it:

"Do the following for each element of the structure array 'data': compare field 'test' of the structure with string 'inverse_real'. Store the results to an array 'select'."</description>
		<content:encoded><![CDATA[<p>OysterEngineer,</p>
<p>I second Loren&#8217;s suggestion on giving anonymous and nested functions a try. It took me a little while to get used to the new syntax for anonymous function, but just like any other MATLAB syntax, I am completely used to it now. I had VERY little programming experience when I started using MATLAB (I may have programmed a little in BASIC), so the learning curve was steep, but after that, MATLAB code was MATLAB code, including anonymous functions.</p>
<p>Another relevant example is the new Object Oriented Programming capability that was introduced in R2008a. It&#8217;s a completely new programming construct, which may seem extremely foreign to a traditional MATLAB user. I was one of those people. I had no experience in object oriented programming, so I was very confused. I think it took me about a year to get my head around it. Now, it feels very natural, and I love it. It changed how I code in MATLAB, and I can create more sophisticated applications with it.</p>
<p>I think we should not dismiss a particular feature just because it is a foreign concept or syntax. Although, I do agree that there should be added benefits for any new programming constructs. I hope that some of the discussions in this post, as well as the other posts by Loren, will convince you of the benefits of anonymous functions. I do recommend giving them a try.</p>
<p>p.s. BTW, I find Steve&#8217;s example with the anonymous function so much easier to understand, because I know how to interpret that syntax. This is how you would read it:</p>
<p>&#8220;Do the following for each element of the structure array &#8216;data&#8217;: compare field &#8216;test&#8217; of the structure with string &#8216;inverse_real&#8217;. Store the results to an array &#8217;select&#8217;.&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: OysterEngineer</title>
		<link>http://blogs.mathworks.com/loren/2009/03/13/using-str2func-for-anonymous-function-handle-creation/#comment-30810</link>
		<dc:creator>OysterEngineer</dc:creator>
		<pubDate>Tue, 17 Nov 2009 22:43:56 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2009/03/13/using-str2func-for-anonymous-function-handle-creation/#comment-30810</guid>
		<description>I guess the types of technical computing problems you work on are so different from the ones I work on that I'm having trouble following your thinking.

Yes, I'll put some effort into this.  Sounds like a fun way to spend the Thanksgiving weekend.</description>
		<content:encoded><![CDATA[<p>I guess the types of technical computing problems you work on are so different from the ones I work on that I&#8217;m having trouble following your thinking.</p>
<p>Yes, I&#8217;ll put some effort into this.  Sounds like a fun way to spend the Thanksgiving weekend.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2009/03/13/using-str2func-for-anonymous-function-handle-creation/#comment-30808</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Tue, 17 Nov 2009 21:04:17 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2009/03/13/using-str2func-for-anonymous-function-handle-creation/#comment-30808</guid>
		<description>OysterEngineer-

More thoughts from me...

* Another reason I like nested and anonymous functions.  This particular discourse will concentrate on anonymous functions mostly, but nested and anonymous functions are so closely related that they points work for both.

Nested/anonymous functions allow for more natural APIs/interfaces.  Here's a quick example.  If I want to work with a bunch of lines, ones for which I don't know the slope and intercept, I have to write a function that takes 3 inputs (x,m,b).  But if what I am doing with these lines is integrating, for example, I really need to have the function be a function of x alone, and not include slope/intercept.  

No programming languages that I am familiar with allow you to separate parameters (slope/intercept) from unknowns (or variables).  They are all just variables.  But, if I have a line defined as a function of x, I can ask all kinds of questions about it without having to myself know slope and intercept.  For that matter, I can substitute something that's not strictly a line.  Suppose I want to next see what happens with a quadratic.  If I use the paradigm of having to write a function, I have to write it now to include, 3 terms, and I have to change my integrator to accept 3 optional arguments, etc.

More elegantly, I can supply a function handle that has only x as an input, and who cares what the underlying function is.  That anonymous function carries with it all the information it needs, and the function I send it to doesn't need to know anything other than it is a function of x.  Gets all the parameter messiness out of the way.

* Finally, I recommend you "put some skin in the game" - i.e., read (or reread) posts from this blog under the category Function Handles, download the code or copy/paste it into an editor and try it.  There are examples in the documentation.  Copy/paste them into files, run them, go through them with a debugger, etc.  I don't think you can replicate my 3 weeks of learning by reading a doc alone.  Take the plunge.

--Loren</description>
		<content:encoded><![CDATA[<p>OysterEngineer-</p>
<p>More thoughts from me&#8230;</p>
<p>* Another reason I like nested and anonymous functions.  This particular discourse will concentrate on anonymous functions mostly, but nested and anonymous functions are so closely related that they points work for both.</p>
<p>Nested/anonymous functions allow for more natural APIs/interfaces.  Here&#8217;s a quick example.  If I want to work with a bunch of lines, ones for which I don&#8217;t know the slope and intercept, I have to write a function that takes 3 inputs (x,m,b).  But if what I am doing with these lines is integrating, for example, I really need to have the function be a function of x alone, and not include slope/intercept.  </p>
<p>No programming languages that I am familiar with allow you to separate parameters (slope/intercept) from unknowns (or variables).  They are all just variables.  But, if I have a line defined as a function of x, I can ask all kinds of questions about it without having to myself know slope and intercept.  For that matter, I can substitute something that&#8217;s not strictly a line.  Suppose I want to next see what happens with a quadratic.  If I use the paradigm of having to write a function, I have to write it now to include, 3 terms, and I have to change my integrator to accept 3 optional arguments, etc.</p>
<p>More elegantly, I can supply a function handle that has only x as an input, and who cares what the underlying function is.  That anonymous function carries with it all the information it needs, and the function I send it to doesn&#8217;t need to know anything other than it is a function of x.  Gets all the parameter messiness out of the way.</p>
<p>* Finally, I recommend you &#8220;put some skin in the game&#8221; - i.e., read (or reread) posts from this blog under the category Function Handles, download the code or copy/paste it into an editor and try it.  There are examples in the documentation.  Copy/paste them into files, run them, go through them with a debugger, etc.  I don&#8217;t think you can replicate my 3 weeks of learning by reading a doc alone.  Take the plunge.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve Eddins</title>
		<link>http://blogs.mathworks.com/loren/2009/03/13/using-str2func-for-anonymous-function-handle-creation/#comment-30803</link>
		<dc:creator>Steve Eddins</dc:creator>
		<pubDate>Tue, 17 Nov 2009 18:40:52 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2009/03/13/using-str2func-for-anonymous-function-handle-creation/#comment-30803</guid>
		<description>OysterEngineer&#8212;You raise a number of good questions that I'm interested in addressing.  It will probably be a few days, though, given what the calendar on the screen in front of me looks like at the moment.  I might about it on my blog.  If I do that, I'll post a pointer here.</description>
		<content:encoded><![CDATA[<p>OysterEngineer&mdash;You raise a number of good questions that I&#8217;m interested in addressing.  It will probably be a few days, though, given what the calendar on the screen in front of me looks like at the moment.  I might about it on my blog.  If I do that, I&#8217;ll post a pointer here.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: OysterEngineer</title>
		<link>http://blogs.mathworks.com/loren/2009/03/13/using-str2func-for-anonymous-function-handle-creation/#comment-30802</link>
		<dc:creator>OysterEngineer</dc:creator>
		<pubDate>Tue, 17 Nov 2009 15:36:01 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2009/03/13/using-str2func-for-anonymous-function-handle-creation/#comment-30802</guid>
		<description>Steve,

I didn't mean to imply that your code is distasteful, I just can't understand what it is doing at a glance.  Since I'm one of the more experienced MatLab users around here, if I can't understand a coding technique, I certainly can't expect my co-workers to understand it.  If you think my code example is harder for a novice to follow, tell me why.

My comment regarding the function handle being hidden in plain sight was only in regards to the way the FRPs for these functions of functions are written.

A life-long dear friend of mine (he &#38; I were best men at each other's weddings) has worked for a career as a code developer.  When I complain to him about code that I can't understand, he bluntly counters that that is the reason why professional developers are needed.

But, that model doesn't work for MatLab.  We recognize that all the technical computing functionality we write in .m code could be provided by some custom application written in C or whatnot.  But, the advantages writing it in MatLab outweigh the disadvantages of having amateurs writing the code.  But, we still have to verify that the code does the correct thing &#38; that requires that we be able to read &#38; understand the code.

Like I said before, I would love to understand fully why you smart developers at The MathWorks think function handles &#38; anonymous functions are attractive.  You clearly see something that we don't.  And, this isn't at all like the other type of technical computing decisions we make in the world of MatLab.

For example, I may choose to estimate transfer functions from recorded data using a CZT based routine rather than an FFT based routine.  I understand how to solve the problem each way &#38; make the decision based on subtle points.

In the case of anonymous functions, my co-workers &#38; I just don't get it.  If as Loren says, a user has to play with it for 3 weeks to understand it, then why can't you guys write up a nice training package that leads the user thru this so they can make the decision about using the technique from a position of knowledge?  Instead, I'm forced to not even consider the technique only because I can't imagine why this is attractive.</description>
		<content:encoded><![CDATA[<p>Steve,</p>
<p>I didn&#8217;t mean to imply that your code is distasteful, I just can&#8217;t understand what it is doing at a glance.  Since I&#8217;m one of the more experienced MatLab users around here, if I can&#8217;t understand a coding technique, I certainly can&#8217;t expect my co-workers to understand it.  If you think my code example is harder for a novice to follow, tell me why.</p>
<p>My comment regarding the function handle being hidden in plain sight was only in regards to the way the FRPs for these functions of functions are written.</p>
<p>A life-long dear friend of mine (he &amp; I were best men at each other&#8217;s weddings) has worked for a career as a code developer.  When I complain to him about code that I can&#8217;t understand, he bluntly counters that that is the reason why professional developers are needed.</p>
<p>But, that model doesn&#8217;t work for MatLab.  We recognize that all the technical computing functionality we write in .m code could be provided by some custom application written in C or whatnot.  But, the advantages writing it in MatLab outweigh the disadvantages of having amateurs writing the code.  But, we still have to verify that the code does the correct thing &amp; that requires that we be able to read &amp; understand the code.</p>
<p>Like I said before, I would love to understand fully why you smart developers at The MathWorks think function handles &amp; anonymous functions are attractive.  You clearly see something that we don&#8217;t.  And, this isn&#8217;t at all like the other type of technical computing decisions we make in the world of MatLab.</p>
<p>For example, I may choose to estimate transfer functions from recorded data using a CZT based routine rather than an FFT based routine.  I understand how to solve the problem each way &amp; make the decision based on subtle points.</p>
<p>In the case of anonymous functions, my co-workers &amp; I just don&#8217;t get it.  If as Loren says, a user has to play with it for 3 weeks to understand it, then why can&#8217;t you guys write up a nice training package that leads the user thru this so they can make the decision about using the technique from a position of knowledge?  Instead, I&#8217;m forced to not even consider the technique only because I can&#8217;t imagine why this is attractive.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve Eddins</title>
		<link>http://blogs.mathworks.com/loren/2009/03/13/using-str2func-for-anonymous-function-handle-creation/#comment-30801</link>
		<dc:creator>Steve Eddins</dc:creator>
		<pubDate>Tue, 17 Nov 2009 13:58:56 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2009/03/13/using-str2func-for-anonymous-function-handle-creation/#comment-30801</guid>
		<description>OysterEngineer&#8212;All function handles are introduced with the "@" character.  This is completely consistent, so I don't really see that the appearance of a function handle in the call to arrayfun is "hidden in plain sight."  Also, I find your code as distasteful as you apparently find mine.

;-)</description>
		<content:encoded><![CDATA[<p>OysterEngineer&mdash;All function handles are introduced with the &#8220;@&#8221; character.  This is completely consistent, so I don&#8217;t really see that the appearance of a function handle in the call to arrayfun is &#8220;hidden in plain sight.&#8221;  Also, I find your code as distasteful as you apparently find mine.</p>
<p>;-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: OysterEngineer</title>
		<link>http://blogs.mathworks.com/loren/2009/03/13/using-str2func-for-anonymous-function-handle-creation/#comment-30800</link>
		<dc:creator>OysterEngineer</dc:creator>
		<pubDate>Tue, 17 Nov 2009 03:03:31 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2009/03/13/using-str2func-for-anonymous-function-handle-creation/#comment-30800</guid>
		<description>Another point.

The style of putting the argument list for an anonymous function ahead of the function name rather than after it doesn't make any sense to us.  All normal functions in the world of MatLab have the input arguments after the function name.

Why is it reversed for anonymous functions?</description>
		<content:encoded><![CDATA[<p>Another point.</p>
<p>The style of putting the argument list for an anonymous function ahead of the function name rather than after it doesn&#8217;t make any sense to us.  All normal functions in the world of MatLab have the input arguments after the function name.</p>
<p>Why is it reversed for anonymous functions?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: OysterEngineer</title>
		<link>http://blogs.mathworks.com/loren/2009/03/13/using-str2func-for-anonymous-function-handle-creation/#comment-30799</link>
		<dc:creator>OysterEngineer</dc:creator>
		<pubDate>Tue, 17 Nov 2009 02:48:01 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2009/03/13/using-str2func-for-anonymous-function-handle-creation/#comment-30799</guid>
		<description>Steve,

A couple things.  Reading between the lines, it appears that your use of this feature is from the command line, rather than writing a stand-alone function to process the data.  i.e., you are using MatLab in what I call its supper, wizbang desktop calculator mode.  Then, you wanted to inspect the data_subset in the workspace after you had done this command line processing.  Am I guessing correct?  

Next, from a first reading of the FRP for arrayfun, I can't see from the Description how you are using legal syntax.  Only after reading it several times &#38; jumping over to the FRP for function_handle do I pick up on the subtle detail that your 1st argument in arrayfun is an in-line generation of a function handle.  The fact that that 1st argument is a function handle is almost hidden in plain sight.

Like I said, this is all very confusing to us.  Several of us out here discussed this a bit and we are all equally confused.

The clearest way to generate your index variable, select, is to do it inside some for loops.  Isn't this what you are after?

&lt;pre&gt;

for ii = 1 : size(data, 1)
    for jj = 1 : size(data, 2)
        select(ii, jj) = strcmp(data(ii, jj).test, 'inverse_real';
    end
end
data)subset = data(select);

&lt;/pre&gt;

I suppose for my code, you need to know that data has 2 dimensions.  But, usually I know the data well enough that this isn't a problem.

I do stuff like this from the command line all the time.  Or, I just open an editor window, type it out, highlight it &#38; do a Evaluate Selection.</description>
		<content:encoded><![CDATA[<p>Steve,</p>
<p>A couple things.  Reading between the lines, it appears that your use of this feature is from the command line, rather than writing a stand-alone function to process the data.  i.e., you are using MatLab in what I call its supper, wizbang desktop calculator mode.  Then, you wanted to inspect the data_subset in the workspace after you had done this command line processing.  Am I guessing correct?  </p>
<p>Next, from a first reading of the FRP for arrayfun, I can&#8217;t see from the Description how you are using legal syntax.  Only after reading it several times &amp; jumping over to the FRP for function_handle do I pick up on the subtle detail that your 1st argument in arrayfun is an in-line generation of a function handle.  The fact that that 1st argument is a function handle is almost hidden in plain sight.</p>
<p>Like I said, this is all very confusing to us.  Several of us out here discussed this a bit and we are all equally confused.</p>
<p>The clearest way to generate your index variable, select, is to do it inside some for loops.  Isn&#8217;t this what you are after?</p>
<pre>

for ii = 1 : size(data, 1)
    for jj = 1 : size(data, 2)
        select(ii, jj) = strcmp(data(ii, jj).test, 'inverse_real';
    end
end
data)subset = data(select);
</pre>
<p>I suppose for my code, you need to know that data has 2 dimensions.  But, usually I know the data well enough that this isn&#8217;t a problem.</p>
<p>I do stuff like this from the command line all the time.  Or, I just open an editor window, type it out, highlight it &amp; do a Evaluate Selection.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
