<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168921</link>
    <title>MATLAB Central Newsreader - 'switch' between function handles</title>
    <description>Feed for thread: 'switch' between function handles</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2008 by The MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>The MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Thu, 08 May 2008 09:14:07 -0400</pubDate>
      <title>'switch' between function handles</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168921#430991</link>
      <author>Gatis B</author>
      <description>&amp;nbsp;Hallo,&lt;br&gt;
I have a function that has a parametrized function handle &lt;br&gt;
as one of its inputs. The function passed on to my 'main' &lt;br&gt;
function can be one option from a finite set, but each of &lt;br&gt;
these have different inputs from both inside my 'main' and &lt;br&gt;
from outside as parameters. So I would like to use 'switch' &lt;br&gt;
to evaluate which of my input functions I have, and act &lt;br&gt;
according to that..&lt;br&gt;
&lt;br&gt;
The problem is that my input function is in the form:&lt;br&gt;
@(x1,x2,x3)inpfcn(x1,x2,x3,par1,par2,par3)&lt;br&gt;
&lt;br&gt;
Obviously I cannot switch between function handles, so I am &lt;br&gt;
using func2str, but with (par1,par2,par3) passed from &lt;br&gt;
outside the 'main' function, even that is not enough detect &lt;br&gt;
the right case. I would imagine this is a kind of a &lt;br&gt;
standard problem, but none of the options that 'doc &lt;br&gt;
function_handle' has leads to has provided any reasonable &lt;br&gt;
clue so far. I suppose one way would be to 'strfind' for &lt;br&gt;
what's between the first ')' and the second '(' in&lt;br&gt;
@(x1,x2,x3)inpfcn(x1,x2,x3,par1,par2,par3)&lt;br&gt;
to just get the sting with its name but that seems to &lt;br&gt;
awkward, taking into account that my 'main' is a state-&lt;br&gt;
derivative function used for an ODE solver.&lt;br&gt;
&lt;br&gt;
Thanks, I will be happy about any suggestions.&lt;br&gt;
Regards,&lt;br&gt;
/GB&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 08 May 2008 10:00:23 -0400</pubDate>
      <title>Re: 'switch' between function handles</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168921#430995</link>
      <author>helper </author>
      <description>"Gatis B" &amp;lt;s050995EXTRACHARACTERS@student.dtu.dk&amp;gt; wrote in &lt;br&gt;
message &amp;lt;fvug8v$q0q$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt;  Hallo,&lt;br&gt;
&amp;gt; I have a function that has a parametrized function handle &lt;br&gt;
&amp;gt; as one of its inputs. The function passed on to my 'main' &lt;br&gt;
&amp;gt; function can be one option from a finite set, but each of &lt;br&gt;
&amp;gt; these have different inputs from both inside my 'main' &lt;br&gt;
and &lt;br&gt;
&amp;gt; from outside as parameters. So I would like to &lt;br&gt;
use 'switch' &lt;br&gt;
&amp;gt; to evaluate which of my input functions I have, and act &lt;br&gt;
&amp;gt; according to that..&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The problem is that my input function is in the form:&lt;br&gt;
&amp;gt; @(x1,x2,x3)inpfcn(x1,x2,x3,par1,par2,par3)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Obviously I cannot switch between function handles, so I &lt;br&gt;
am &lt;br&gt;
&amp;gt; using func2str, but with (par1,par2,par3) passed from &lt;br&gt;
&amp;gt; outside the 'main' function, even that is not enough &lt;br&gt;
detect &lt;br&gt;
&amp;gt; the right case. I would imagine this is a kind of a &lt;br&gt;
&amp;gt; standard problem, but none of the options that 'doc &lt;br&gt;
&amp;gt; function_handle' has leads to has provided any reasonable &lt;br&gt;
&amp;gt; clue so far. I suppose one way would be to 'strfind' for &lt;br&gt;
&amp;gt; what's between the first ')' and the second '(' in&lt;br&gt;
&amp;gt; @(x1,x2,x3)inpfcn(x1,x2,x3,par1,par2,par3)&lt;br&gt;
&amp;gt; to just get the sting with its name but that seems to &lt;br&gt;
&amp;gt; awkward, taking into account that my 'main' is a state-&lt;br&gt;
&amp;gt; derivative function used for an ODE solver.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks, I will be happy about any suggestions.&lt;br&gt;
&amp;gt; Regards,&lt;br&gt;
&amp;gt; /GB&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
There are a few things to realize here.  &lt;br&gt;
&lt;br&gt;
First, what you are referring to as a "parametrized &lt;br&gt;
function handle" is actually called an "anonymous &lt;br&gt;
function".  This will help when searching the doc.&lt;br&gt;
&lt;br&gt;
Second, anonymous function are very different than regular &lt;br&gt;
function handles.  They don't have to be in the form you &lt;br&gt;
give (e.g. @(arglist)FUNCNAME(arglist)).  They can be in a &lt;br&gt;
wide range of forms like:&lt;br&gt;
&lt;br&gt;
@(x,y)x.*y&lt;br&gt;
@(x,y)sin(x)*cos(y)&lt;br&gt;
&lt;br&gt;
So if you are looking for a way to determine FUNCNAME as &lt;br&gt;
given in my above example, there is no built-in way to do &lt;br&gt;
this in MATLAB given that the anonymous function might &lt;br&gt;
contain multiple FUNCNAMEs or none.&lt;br&gt;
&lt;br&gt;
You *can* determine the number of input arguments to your &lt;br&gt;
anonymous function using the NARGIN function:&lt;br&gt;
&lt;br&gt;
myfunc = @(x,y,z)x+y+z&lt;br&gt;
nargin(myfunc)&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
I would suggest the most robust way to do what you are &lt;br&gt;
trying to do is to pass additional information to the &lt;br&gt;
function as a way of telling that function about the passed &lt;br&gt;
anonymous function.  For example, if you are currently &lt;br&gt;
doing something like:&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;f1 = @(x,y)FUN1(x,y,p1,p2)&lt;br&gt;
&amp;nbsp;&amp;nbsp;f2 = @(x,y,z)FUN2(x,y,z,p1,p2)&lt;br&gt;
&lt;br&gt;
then calling some other function using:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;result = FUNX(a,b,f1)&lt;br&gt;
&lt;br&gt;
or&lt;br&gt;
&lt;br&gt;
&amp;nbsp;result = FUNX(a,b,f2)&lt;br&gt;
&lt;br&gt;
I would suggest doing something like the following:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;result = FUNX(a,b,f1,1)&lt;br&gt;
or&lt;br&gt;
&amp;nbsp;&amp;nbsp;result = FUNX(a,b,f2,2)&lt;br&gt;
&lt;br&gt;
Then changing FUNX so that it does the SWITCH command on &lt;br&gt;
the 4th input argument which indicates which anonymous &lt;br&gt;
function you sent.&lt;br&gt;
&lt;br&gt;
Or, you can even do something like the following:&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;f1 = {1, @(x,y)FUN1(x,y,p1,p2)}&lt;br&gt;
&amp;nbsp;&amp;nbsp;f2 = {2, @(x,y,z)FUN2(x,y,z,p1,p2)}&lt;br&gt;
&lt;br&gt;
then&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;result = FUNX(a,b,f1)&lt;br&gt;
&lt;br&gt;
or&lt;br&gt;
&lt;br&gt;
&amp;nbsp;result = FUNX(a,b,f2)&lt;br&gt;
&lt;br&gt;
Then, within FUNX, do something like the following:&lt;br&gt;
&lt;br&gt;
function result = FUNX(a,b,FUN)&lt;br&gt;
...&amp;lt;some code&amp;gt;...&lt;br&gt;
switch FUN{1}&lt;br&gt;
case 1&lt;br&gt;
&amp;nbsp;&amp;nbsp;out = FUN{2}(x,y)&lt;br&gt;
case 2&lt;br&gt;
&amp;nbsp;&amp;nbsp;out = FUN{2}(x,y,z)&lt;br&gt;
end&lt;br&gt;
...&amp;lt;more code&amp;gt;....&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
I hope this helps.&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 08 May 2008 11:07:03 -0400</pubDate>
      <title>Re: 'switch' between function handles</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168921#431008</link>
      <author>Gatis B</author>
      <description>"helper " &amp;lt;spamless@nospam.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;fvuivn$71e$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; "Gatis B" &amp;lt;s050995EXTRACHARACTERS@student.dtu.dk&amp;gt; wrote &lt;br&gt;
in &lt;br&gt;
&amp;gt; message &amp;lt;fvug8v$q0q$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt;  Hallo,&lt;br&gt;
&amp;gt; &amp;gt; I have a function that has a parametrized function &lt;br&gt;
handle &lt;br&gt;
&amp;gt; &amp;gt; as one of its inputs. The function passed on to &lt;br&gt;
my 'main' &lt;br&gt;
&amp;gt; &amp;gt; function can be one option from a finite set, but each &lt;br&gt;
of &lt;br&gt;
&amp;gt; &amp;gt; these have different inputs from both inside my 'main' &lt;br&gt;
&amp;gt; and &lt;br&gt;
&amp;gt; &amp;gt; from outside as parameters. So I would like to &lt;br&gt;
&amp;gt; use 'switch' &lt;br&gt;
&amp;gt; &amp;gt; to evaluate which of my input functions I have, and act &lt;br&gt;
&amp;gt; &amp;gt; according to that..&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; The problem is that my input function is in the form:&lt;br&gt;
&amp;gt; &amp;gt; @(x1,x2,x3)inpfcn(x1,x2,x3,par1,par2,par3)&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Obviously I cannot switch between function handles, so &lt;br&gt;
I &lt;br&gt;
&amp;gt; am &lt;br&gt;
&amp;gt; &amp;gt; using func2str, but with (par1,par2,par3) passed from &lt;br&gt;
&amp;gt; &amp;gt; outside the 'main' function, even that is not enough &lt;br&gt;
&amp;gt; detect &lt;br&gt;
&amp;gt; &amp;gt; the right case. I would imagine this is a kind of a &lt;br&gt;
&amp;gt; &amp;gt; standard problem, but none of the options that 'doc &lt;br&gt;
&amp;gt; &amp;gt; function_handle' has leads to has provided any &lt;br&gt;
reasonable &lt;br&gt;
&amp;gt; &amp;gt; clue so far. I suppose one way would be to 'strfind' &lt;br&gt;
for &lt;br&gt;
&amp;gt; &amp;gt; what's between the first ')' and the second '(' in&lt;br&gt;
&amp;gt; &amp;gt; @(x1,x2,x3)inpfcn(x1,x2,x3,par1,par2,par3)&lt;br&gt;
&amp;gt; &amp;gt; to just get the sting with its name but that seems to &lt;br&gt;
&amp;gt; &amp;gt; awkward, taking into account that my 'main' is a state-&lt;br&gt;
&amp;gt; &amp;gt; derivative function used for an ODE solver.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Thanks, I will be happy about any suggestions.&lt;br&gt;
&amp;gt; &amp;gt; Regards,&lt;br&gt;
&amp;gt; &amp;gt; /GB&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; There are a few things to realize here.  &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; First, what you are referring to as a "parametrized &lt;br&gt;
&amp;gt; function handle" is actually called an "anonymous &lt;br&gt;
&amp;gt; function".  This will help when searching the doc.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Second, anonymous function are very different than &lt;br&gt;
regular &lt;br&gt;
&amp;gt; function handles.  They don't have to be in the form you &lt;br&gt;
&amp;gt; give (e.g. @(arglist)FUNCNAME(arglist)).  They can be in &lt;br&gt;
a &lt;br&gt;
&amp;gt; wide range of forms like:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; @(x,y)x.*y&lt;br&gt;
&amp;gt; @(x,y)sin(x)*cos(y)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So if you are looking for a way to determine FUNCNAME as &lt;br&gt;
&amp;gt; given in my above example, there is no built-in way to do &lt;br&gt;
&amp;gt; this in MATLAB given that the anonymous function might &lt;br&gt;
&amp;gt; contain multiple FUNCNAMEs or none.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; You *can* determine the number of input arguments to your &lt;br&gt;
&amp;gt; anonymous function using the NARGIN function:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; myfunc = @(x,y,z)x+y+z&lt;br&gt;
&amp;gt; nargin(myfunc)&lt;br&gt;
&amp;gt; ans =&lt;br&gt;
&amp;gt;        3&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I would suggest the most robust way to do what you are &lt;br&gt;
&amp;gt; trying to do is to pass additional information to the &lt;br&gt;
&amp;gt; function as a way of telling that function about the &lt;br&gt;
passed &lt;br&gt;
&amp;gt; anonymous function.  For example, if you are currently &lt;br&gt;
&amp;gt; doing something like:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   f1 = @(x,y)FUN1(x,y,p1,p2)&lt;br&gt;
&amp;gt;   f2 = @(x,y,z)FUN2(x,y,z,p1,p2)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; then calling some other function using:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   result = FUNX(a,b,f1)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; or&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;  result = FUNX(a,b,f2)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I would suggest doing something like the following:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   result = FUNX(a,b,f1,1)&lt;br&gt;
&amp;gt; or&lt;br&gt;
&amp;gt;   result = FUNX(a,b,f2,2)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Then changing FUNX so that it does the SWITCH command on &lt;br&gt;
&amp;gt; the 4th input argument which indicates which anonymous &lt;br&gt;
&amp;gt; function you sent.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Or, you can even do something like the following:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   f1 = {1, @(x,y)FUN1(x,y,p1,p2)}&lt;br&gt;
&amp;gt;   f2 = {2, @(x,y,z)FUN2(x,y,z,p1,p2)}&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; then&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   result = FUNX(a,b,f1)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; or&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;  result = FUNX(a,b,f2)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Then, within FUNX, do something like the following:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; function result = FUNX(a,b,FUN)&lt;br&gt;
&amp;gt; ...&amp;lt;some code&amp;gt;...&lt;br&gt;
&amp;gt; switch FUN{1}&lt;br&gt;
&amp;gt; case 1&lt;br&gt;
&amp;gt;   out = FUN{2}(x,y)&lt;br&gt;
&amp;gt; case 2&lt;br&gt;
&amp;gt;   out = FUN{2}(x,y,z)&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; ...&amp;lt;more code&amp;gt;....&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I hope this helps.&lt;br&gt;
&lt;br&gt;
Thanks a lot, the option with cells looks good enough. Yes, &lt;br&gt;
and it was kind of important to know that there was no in-&lt;br&gt;
built way.&lt;br&gt;
/GB&lt;br&gt;
&lt;br&gt;
</description>
    </item>
  </channel>
</rss>
