<?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: Some Ways to Create Function Handles</title>
	<atom:link href="http://blogs.mathworks.com/loren/2006/11/17/some-ways-to-create-function-handles/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2006/11/17/some-ways-to-create-function-handles/</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: Carmen</title>
		<link>http://blogs.mathworks.com/loren/2006/11/17/some-ways-to-create-function-handles/#comment-30507</link>
		<dc:creator>Carmen</dc:creator>
		<pubDate>Sat, 01 Aug 2009 17:55:08 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=66#comment-30507</guid>
		<description>I&#039;m strugling with the error message:
‘Nonscalar arrays of function handles are not allowed; use cell arrays instead’
as well... please help me out with this application.
This is a modified version of my code part of my code:
&lt;pre&gt;
for j=1:10;
    for i=1:10;
        aprox(i,j)=schumaker(vv(:,i,j),nod,agrid);
        aprox2(i,j)=@(x) ppval(aprox(i,j),x);
        a(i,j)=agrid(1);
        b(i,j)= agrid(10);
       [policya(i,j),vvaprox(i,j)]=golden(aprox2(i,j),a(i,j),b(i,j));
end;
end;
&lt;/pre&gt;
where both schumaker and golden are two functions defined in separate m-files. When I used the command:
 aprox2(i,j)=@(x) ppval(aprox(i,j),x);
I get an error message instead of a warning as I did with the older version of matlab... I can&#039;t figure out how to fix this. 

Thanks in advance!</description>
		<content:encoded><![CDATA[<p>I&#8217;m strugling with the error message:<br />
‘Nonscalar arrays of function handles are not allowed; use cell arrays instead’<br />
as well&#8230; please help me out with this application.<br />
This is a modified version of my code part of my code:</p>
<pre>
for j=1:10;
    for i=1:10;
        aprox(i,j)=schumaker(vv(:,i,j),nod,agrid);
        aprox2(i,j)=@(x) ppval(aprox(i,j),x);
        a(i,j)=agrid(1);
        b(i,j)= agrid(10);
       [policya(i,j),vvaprox(i,j)]=golden(aprox2(i,j),a(i,j),b(i,j));
end;
end;
</pre>
<p>where both schumaker and golden are two functions defined in separate m-files. When I used the command:<br />
 aprox2(i,j)=@(x) ppval(aprox(i,j),x);<br />
I get an error message instead of a warning as I did with the older version of matlab&#8230; I can&#8217;t figure out how to fix this. </p>
<p>Thanks in advance!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/11/17/some-ways-to-create-function-handles/#comment-30456</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Mon, 06 Jul 2009 11:02:43 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=66#comment-30456</guid>
		<description>Veronika-

I recommend following the advice of the error message.  Place them in a cell array. E.g.,

&lt;pre&gt;
fh = { @sin, @cos};
x = 0:.01:1;
ysin = fh{1}(x);
&lt;/pre&gt;

--Loren</description>
		<content:encoded><![CDATA[<p>Veronika-</p>
<p>I recommend following the advice of the error message.  Place them in a cell array. E.g.,</p>
<pre>
fh = { @sin, @cos};
x = 0:.01:1;
ysin = fh{1}(x);
</pre>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Veronika</title>
		<link>http://blogs.mathworks.com/loren/2006/11/17/some-ways-to-create-function-handles/#comment-30455</link>
		<dc:creator>Veronika</dc:creator>
		<pubDate>Mon, 06 Jul 2009 10:15:52 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=66#comment-30455</guid>
		<description>Hi!

MATLAB R2009a corresponds an error &#039;Nonscalar arrays of function handles are not allowed; use cell arrays instead&#039;, but it was working before? Is there some common solution how should i change the script to get it working?

Thanks for answers in advance!</description>
		<content:encoded><![CDATA[<p>Hi!</p>
<p>MATLAB R2009a corresponds an error &#8216;Nonscalar arrays of function handles are not allowed; use cell arrays instead&#8217;, but it was working before? Is there some common solution how should i change the script to get it working?</p>
<p>Thanks for answers in advance!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ljubomir Josifovski</title>
		<link>http://blogs.mathworks.com/loren/2006/11/17/some-ways-to-create-function-handles/#comment-30401</link>
		<dc:creator>Ljubomir Josifovski</dc:creator>
		<pubDate>Thu, 18 Jun 2009 10:49:07 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=66#comment-30401</guid>
		<description>Thanks Loren. Since I wrote my post I discovered in the help the &quot;functions&quot; function, which spells out the info the handle contains.

Regards,
Ljubomir</description>
		<content:encoded><![CDATA[<p>Thanks Loren. Since I wrote my post I discovered in the help the &#8220;functions&#8221; function, which spells out the info the handle contains.</p>
<p>Regards,<br />
Ljubomir</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/11/17/some-ways-to-create-function-handles/#comment-30400</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Thu, 18 Jun 2009 10:42:24 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=66#comment-30400</guid>
		<description>Ljubomir-

You can&#039;t make the function handle local to the file. If you pass a function handle pointing to one file&#039;s subfunction, then that subfunction will be called regardless of where the function handle is passed and whether or not that new context has a function of the same name in its scope.  To get that behavior you have to use the strings as you note.

--Loren</description>
		<content:encoded><![CDATA[<p>Ljubomir-</p>
<p>You can&#8217;t make the function handle local to the file. If you pass a function handle pointing to one file&#8217;s subfunction, then that subfunction will be called regardless of where the function handle is passed and whether or not that new context has a function of the same name in its scope.  To get that behavior you have to use the strings as you note.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ljubomir Josifovski</title>
		<link>http://blogs.mathworks.com/loren/2006/11/17/some-ways-to-create-function-handles/#comment-30399</link>
		<dc:creator>Ljubomir Josifovski</dc:creator>
		<pubDate>Thu, 18 Jun 2009 10:28:13 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=66#comment-30399</guid>
		<description>Are there maybe function handles that are &quot;local&quot; to an .m file?

Example, if I have

in file qwe1.m:
&lt;pre&gt;
  function qwe1()
    q.fh = @qwe;
    save(&#039;qwe1&#039;, &#039;q&#039;);
    qq = load(&#039;qwe1&#039;);
    qq.q.fh();
  end
  function qwe()
    disp(&#039;qwe from qwe1 called&#039;)
  end
&lt;/pre&gt;

and in file qwe2.m:
&lt;pre&gt;
  function qwe2()
    qq = load(&#039;qwe1&#039;);
    qq.q.fh();
  end
  function qwe()
    disp(&#039;qwe from qwe2 called&#039;)
  end
&lt;/pre&gt;

Then in Matlab Version 7.5.0.342 (R2007b) I get

&lt;pre&gt;
  &gt;&gt; qwe1
  qwe from qwe1 called
  &gt;&gt; qwe2
  qwe from qwe1 called
&lt;/pre&gt;

i.e. the function handle de-serialised in qwe2 &quot;remembers&quot; it points to (private) function qwe in qwe1, not qwe2.

I was wondering if it is possible to make the handle local to the source file? Like if a was saving &#039;qwe&#039; as a string and used feval latter to call the (private) function qwe when running qwe2 I would have called the function qwe from qwe2. :-) 

Not sure if I am explaining what I know well, here is an example,

in file qwe3.m:
&lt;pre&gt;
  function qwe3
    q.fs = &#039;qwe&#039;;
    save(&#039;qwe3&#039;, &#039;q&#039;);
    qq = load(&#039;qwe3&#039;);
    feval(qq.q.fs);
  end
  function qwe()
    disp(&#039;qwe from qwe3 called&#039;)
  end
&lt;/pre&gt;

in file qwe4.m:
&lt;pre&gt;
  function qwe4()
    qq = load(&#039;qwe3&#039;);
    feval(qq.q.fs);
  end
  function qwe()
    disp(&#039;qwe from qwe4 called&#039;)
  end
&lt;/pre&gt;

Now when I run in Matlab
&lt;pre&gt;
  &gt;&gt; qwe3
  qwe from qwe3 called
  &gt;&gt; qwe4
  qwe from qwe4 called
&lt;/pre&gt;

i.e. the 2nd call goes to the qwe function in file qwe4.m (while if this were a file handle would have gone to qwe in qwe3.m).

Thanks for you help,
Ljubomir Josifovski</description>
		<content:encoded><![CDATA[<p>Are there maybe function handles that are &#8220;local&#8221; to an .m file?</p>
<p>Example, if I have</p>
<p>in file qwe1.m:</p>
<pre>
  function qwe1()
    q.fh = @qwe;
    save('qwe1', 'q');
    qq = load('qwe1');
    qq.q.fh();
  end
  function qwe()
    disp('qwe from qwe1 called')
  end
</pre>
<p>and in file qwe2.m:</p>
<pre>
  function qwe2()
    qq = load('qwe1');
    qq.q.fh();
  end
  function qwe()
    disp('qwe from qwe2 called')
  end
</pre>
<p>Then in Matlab Version 7.5.0.342 (R2007b) I get</p>
<pre>
  &gt;&gt; qwe1
  qwe from qwe1 called
  &gt;&gt; qwe2
  qwe from qwe1 called
</pre>
<p>i.e. the function handle de-serialised in qwe2 &#8220;remembers&#8221; it points to (private) function qwe in qwe1, not qwe2.</p>
<p>I was wondering if it is possible to make the handle local to the source file? Like if a was saving &#8216;qwe&#8217; as a string and used feval latter to call the (private) function qwe when running qwe2 I would have called the function qwe from qwe2. :-) </p>
<p>Not sure if I am explaining what I know well, here is an example,</p>
<p>in file qwe3.m:</p>
<pre>
  function qwe3
    q.fs = 'qwe';
    save('qwe3', 'q');
    qq = load('qwe3');
    feval(qq.q.fs);
  end
  function qwe()
    disp('qwe from qwe3 called')
  end
</pre>
<p>in file qwe4.m:</p>
<pre>
  function qwe4()
    qq = load('qwe3');
    feval(qq.q.fs);
  end
  function qwe()
    disp('qwe from qwe4 called')
  end
</pre>
<p>Now when I run in Matlab</p>
<pre>
  &gt;&gt; qwe3
  qwe from qwe3 called
  &gt;&gt; qwe4
  qwe from qwe4 called
</pre>
<p>i.e. the 2nd call goes to the qwe function in file qwe4.m (while if this were a file handle would have gone to qwe in qwe3.m).</p>
<p>Thanks for you help,<br />
Ljubomir Josifovski</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Grunde Waag</title>
		<link>http://blogs.mathworks.com/loren/2006/11/17/some-ways-to-create-function-handles/#comment-30374</link>
		<dc:creator>Grunde Waag</dc:creator>
		<pubDate>Tue, 09 Jun 2009 12:37:36 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=66#comment-30374</guid>
		<description>Thanks a lot.
Didn&#039;t realize that I can call the function with fn(obj) instead of obj.fn().

I find function handles very useful. I use them when I want the code to be flexible. For instance if I want to parse some text with headers. When the parser finds a header &#039;HEI&#039; with some corresponding data, it calls the function parseHEI with the data as argument. This makes the code flexible. Whenever I need a new header I just implement the parsheNEWHEADER function and the everything else is ready to go.</description>
		<content:encoded><![CDATA[<p>Thanks a lot.<br />
Didn&#8217;t realize that I can call the function with fn(obj) instead of obj.fn().</p>
<p>I find function handles very useful. I use them when I want the code to be flexible. For instance if I want to parse some text with headers. When the parser finds a header &#8216;HEI&#8217; with some corresponding data, it calls the function parseHEI with the data as argument. This makes the code flexible. Whenever I need a new header I just implement the parsheNEWHEADER function and the everything else is ready to go.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/11/17/some-ways-to-create-function-handles/#comment-30371</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Mon, 08 Jun 2009 14:32:17 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=66#comment-30371</guid>
		<description>Grunde-

Take a look at &lt;a href=&quot;http://www.mathworks.com/access/helpdesk/help/techdoc/ref/str2func.html&quot; rel=&quot;nofollow&quot;&gt;str2func&lt;/a&gt;.  Also in your hei method, just create the function handle you want: fh = @hopp.  When you call it with an object of the correct class, the method will get called.

--Loren</description>
		<content:encoded><![CDATA[<p>Grunde-</p>
<p>Take a look at <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/str2func.html" rel="nofollow">str2func</a>.  Also in your hei method, just create the function handle you want: fh = @hopp.  When you call it with an object of the correct class, the method will get called.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Grunde Waag</title>
		<link>http://blogs.mathworks.com/loren/2006/11/17/some-ways-to-create-function-handles/#comment-30370</link>
		<dc:creator>Grunde Waag</dc:creator>
		<pubDate>Mon, 08 Jun 2009 13:53:17 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=66#comment-30370</guid>
		<description>I don&#039;t know the function name on before hand. I just have a string.</description>
		<content:encoded><![CDATA[<p>I don&#8217;t know the function name on before hand. I just have a string.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Grunde Waag</title>
		<link>http://blogs.mathworks.com/loren/2006/11/17/some-ways-to-create-function-handles/#comment-30369</link>
		<dc:creator>Grunde Waag</dc:creator>
		<pubDate>Mon, 08 Jun 2009 13:49:18 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=66#comment-30369</guid>
		<description>Is there a way of making a function handle to a class function?

&lt;pre&gt;
methods
   function hei(obj)
     % make function handle to obj.hopp()???
   end

   function hopp(obj,p)
     disp(p)
   end
end
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Is there a way of making a function handle to a class function?</p>
<pre>
methods
   function hei(obj)
     % make function handle to obj.hopp()???
   end

   function hopp(obj,p)
     disp(p)
   end
end
</pre>
]]></content:encoded>
	</item>
</channel>
</rss>

