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
"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>....
"helper " <spamless@nospam.com> wrote in message
<fvuivn$71e$1@fred.mathworks.com>...
> "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.
Thanks a lot, the option with cells looks good enough. Yes,
and it was kind of important to know that there was no in-
built way.
/GB
Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for
all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content.
Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available
via MATLAB Central. Read the complete Disclaimer prior to use.