help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using STR2FUNC in sub-function to refer to other sub-function(s)


From: Jaroslav Hajek
Subject: Re: Using STR2FUNC in sub-function to refer to other sub-function(s)
Date: Mon, 14 Sep 2009 11:41:19 +0200

On Fri, Sep 11, 2009 at 4:04 PM, Bård Skaflestad
<address@hidden> wrote:
> All,
>
> I'm experiencing a bit of bother using the STR2FUNC function.  For sake of 
> argument I'll use the synthetic function 'foo' defined as
>
> $ cat foo.m
>   function h = foo()
>      h = @sub1;
>   end
>
>   function h = sub1()
>      h = str2func('sub2');
>   end
>
>   function h = sub2()
>      h = @(x) sub3(x, 10);
>   end
>
>   function q = sub3(a, b)
>      q = fprintf('sub3: (a,b) = (%f,%d)\n', a, b);
>   end
> $
>
> In Octave I then get
>
> --------------------8<----------------------------------------8<-------------------------
>
>    octave:29> f = foo(), s1 = f(), s2 = s1(), s3 = s2(pi)
>    f =
>
>    sub1
>
>    error: error creating function handle "@sub2"
>    error: called from:
>    error:   /home/bska/tmp/build/octave/3.2.2/foo.m at line 6, column 9
>
> --------------------8<----------------------------------------8<-------------------------
>
> while MATLAB yields the, to me, expected output
>
> --------------------8<----------------------------------------8<-------------------------
>
>    >> f = foo(), s1 = f(), s2 = s1(), s3 = s2(pi)
>
>    f =
>
>       address@hidden
>
>    s1 =
>
>       address@hidden
>
>    s2 =
>
>        @(x)sub3(x,10)
>
>    sub3: (a,b) = (3.141593,10)
>
>    s3 =
>
>        28
>
> --------------------8<----------------------------------------8<-------------------------
>
> My question is if this (failing to construct a function handle to 'sub2') is 
> an error in Octave or if Octave's STR2FUNC implementation is not expected to 
> have the ability to construct handles to (other) sub-functions of a "main" 
> function.  I should note that if I replace the above 'sub1' function by the 
> (equivalent, in this case)
>
>    function h = sub1()
>       h = @sub2;
>    end
>
> the above error goes away.  In the general case, however, I would really like 
> the ability to use STR2FUNC here because the function name is not generally 
> known until runtime in code.  Admittedly, my code uses only a bounded set of 
> possible string values (roughly 5-10 alternatives), but I'd rather not insert 
> a big SWITCH statement in there.  Any and all suggestions will be greatly 
> appreciated.
>
>
> Best regards,
> --
> Bård Skaflestad                            <address@hidden>
> SINTEF ICT, Applied mathematics
> Forskningsveien 1, No-0314 Oslo
> Tel.: (+47) 2206 7642
>
>

The following patch fixes the problem in the development tree:
http://hg.savannah.gnu.org/hgweb/octave/rev/8d79f36ebdde

this patch is not directly applicable to 3.2.x (breaks ABI), so it
will need a separate patch. It may not make it into 3.2.3 which is
just days away.

thanks

-- 
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz



reply via email to

[Prev in Thread] Current Thread [Next in Thread]