Path: news.mathworks.com!not-for-mail
From: "helper " <spamless@nospam.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: 'switch' between function handles
Date: Thu, 8 May 2008 10:00:23 +0000 (UTC)
Organization: Timothy S. Farajian, Inc.
Lines: 120
Message-ID: <fvuivn$71e$1@fred.mathworks.com>
References: <fvug8v$q0q$1@fred.mathworks.com>
Reply-To: "helper " <spamless@nospam.com>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1210240823 7214 172.30.248.35 (8 May 2008 10:00:23 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 8 May 2008 10:00:23 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1272923
Xref: news.mathworks.com comp.soft-sys.matlab:467341


"Gatis B" <s050995EXTRACHARACTERS@student.dtu.dk> wrote in 
message <fvug8v$q0q$1@fred.mathworks.com>...
>  Hallo,
> I have a function that has a parametrized function handle 
> as one of its inputs. The function passed on to my 'main' 
> function can be one option from a finite set, but each of 
> these have different inputs from both inside my 'main' 
and 
> from outside as parameters. So I would like to 
use 'switch' 
> to evaluate which of my input functions I have, and act 
> according to that..
> 
> The problem is that my input function is in the form:
> @(x1,x2,x3)inpfcn(x1,x2,x3,par1,par2,par3)
> 
> Obviously I cannot switch between function handles, so I 
am 
> using func2str, but with (par1,par2,par3) passed from 
> outside the 'main' function, even that is not enough 
detect 
> the right case. I would imagine this is a kind of a 
> standard problem, but none of the options that 'doc 
> function_handle' has leads to has provided any reasonable 
> clue so far. I suppose one way would be to 'strfind' for 
> what's between the first ')' and the second '(' in
> @(x1,x2,x3)inpfcn(x1,x2,x3,par1,par2,par3)
> to just get the sting with its name but that seems to 
> awkward, taking into account that my 'main' is a state-
> derivative function used for an ODE solver.
> 
> Thanks, I will be happy about any suggestions.
> Regards,
> /GB


There are a few things to realize here.  

First, what you are referring to as a "parametrized 
function handle" is actually called an "anonymous 
function".  This will help when searching the doc.

Second, anonymous function are very different than regular 
function handles.  They don't have to be in the form you 
give (e.g. @(arglist)FUNCNAME(arglist)).  They can be in a 
wide range of forms like:

@(x,y)x.*y
@(x,y)sin(x)*cos(y)

So if you are looking for a way to determine FUNCNAME as 
given in my above example, there is no built-in way to do 
this in MATLAB given that the anonymous function might 
contain multiple FUNCNAMEs or none.

You *can* determine the number of input arguments to your 
anonymous function using the NARGIN function:

myfunc = @(x,y,z)x+y+z
nargin(myfunc)
ans =
       3


I would suggest the most robust way to do what you are 
trying to do is to pass additional information to the 
function as a way of telling that function about the passed 
anonymous function.  For example, if you are currently 
doing something like:


  f1 = @(x,y)FUN1(x,y,p1,p2)
  f2 = @(x,y,z)FUN2(x,y,z,p1,p2)

then calling some other function using:

  result = FUNX(a,b,f1)

or

 result = FUNX(a,b,f2)

I would suggest doing something like the following:

  result = FUNX(a,b,f1,1)
or
  result = FUNX(a,b,f2,2)

Then changing FUNX so that it does the SWITCH command on 
the 4th input argument which indicates which anonymous 
function you sent.

Or, you can even do something like the following:


  f1 = {1, @(x,y)FUN1(x,y,p1,p2)}
  f2 = {2, @(x,y,z)FUN2(x,y,z,p1,p2)}

then

  result = FUNX(a,b,f1)

or

 result = FUNX(a,b,f2)

Then, within FUNX, do something like the following:

function result = FUNX(a,b,FUN)
...<some code>...
switch FUN{1}
case 1
  out = FUN{2}(x,y)
case 2
  out = FUN{2}(x,y,z)
end
...<more code>....


I hope this helps.