help-octave
[Top][All Lists]

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

 From: Bård Skaflestad Subject: Using STR2FUNC in sub-function to refer to other sub-function(s) Date: Fri, 11 Sep 2009 16:04:30 +0200

```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 =

@sub1

s1 =

@sub2

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,
--
SINTEF ICT, Applied mathematics
Forskningsveien 1, No-0314 Oslo
Tel.: (+47) 2206 7642

For the record, I have

--------------------8<----------------------------------------8<-------------------------

octave:26> ver
----------------------------------------------------------------------
GNU Octave Version 3.2.2
Operating System: Linux 2.6.22.14-72.fc6 #1 SMP Wed Nov 21 14:10:25 EST
2007 x86_64
----------------------------------------------------------------------
no packages installed.
octave:27>

--------------------8<----------------------------------------8<-------------------------

The interpreter is freshly built today (Friday the 11th of September 2009), and
configured as

--------------------8<----------------------------------------8<-------------------------

--prefix='/home/bska/system/usr/local'
--libdir='/home/bska/system/usr/local/lib64' CXX='g++43' CC='gcc43'
F77='gfortran43' FC='gfortran43'
CPPFLAGS='-I/home/bska/system/usr/local/include
-I/home/bska/system/usr/local/include/SuiteSparse'
LDFLAGS='-L/home/bska/system/usr/local/lib64 -L/usr/local/lib64 -L/usr/lib64
-L/lib64' LIBS='-lmetis'

--------------------8<----------------------------------------8<-------------------------

whence 'configure' reports

--------------------8<----------------------------------------8<-------------------------

Octave is now configured for x86_64-unknown-linux-gnu

Installation prefix:  /home/bska/system/usr/local
C compiler:           gcc43   -Wall -W -Wshadow -Wformat -g -O2
C++ compiler:         g++43   -Wall -W -Wshadow -Wold-style-cast -Wformat -g
-O2
Fortran compiler:     gfortran43 -O
Fortran libraries:     -L/home/bska/system/usr/local/lib64 -L/usr/local/lib64
-L/usr/lib64 -L/lib64 -L/usr/local/lib/gcc/x86_64-unknown-linux-gnu/4.3.3
-L/usr/local/lib/gcc/x86_64-unknown-linux-gnu/4.3.3/../../../../lib64 -
L/lib/../lib64 -L/usr/lib/../lib64
-L/usr/local/lib/gcc/x86_64-unknown-linux-gnu/4.3.3/../../.. -lhdf5 -lz
-lgfortranbegin -lgfortran -lm -lmetis
BLAS libraries:       -llapack -lblas
FFTW libraries:       -lfftw3 -lfftw3f
GLPK libraries:
UMFPACK libraries:    -lumfpack
AMD libraries:        -lamd
CAMD libraries:       -lcamd
COLAMD libraries:     -lcolamd
CCOLAMD libraries:    -lccolamd
CHOLMOD libraries:    -lcholmod
CXSPARSE libraries:   -lcxsparse
ARPACK libraries:     -larpack
QRUPDATE libraries:   -lqrupdate
HDF5 libraries:       -lhdf5
CURL libraries:
REGEX libraries:      -L/home/bska/system/usr/local/lib64 -lpcre
QHULL libraries:      -lqhull
OPENGL libraries:
FLTK backend libs:
X11 include flags:
X11 libraries:
CARBON libraries:
LIBS:                 -lreadline  -lncurses -ldl -lblas -lhdf5 -lz -lm -lmetis
Default pager:        less
gnuplot:              gnuplot
Magick config:

Do internal array bounds checking:  false
Build static libraries:             false
Build shared libraries:             true
Include support for GNU readline:   true
64-bit array dims and indexing:     false

configure:34470: WARNING: I didn't find gperf, but it's only a problem if you
need to reconstruct oct-gperf.h
solving linear programs will be disabled.
GraphicsMagic++ library and header files are missing, so imread will not be
fully functional
configure:34614: WARNING: I didn't find texi2dvi, but it's only a problem if
you need to reconstruct the DVI version of the manual
be disabled."
configure:34717: WARNING:
configure:34719: WARNING: I didn't find the necessary libraries to compile
native
configure:34721: WARNING: graphics. It isn't necessary to have native graphics
configure:34723: WARNING: but you will have to use gnuplot or you won't be able
configure:34725: WARNING: to use any of Octave's plotting commands
configure:34727: WARNING:
configure:34737:

NOTE: libraries may be skipped if a library is not found OR
if the library on your system is missing required features.

--------------------8<----------------------------------------8<-------------------------

```