[Top][All Lists]

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

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


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;

   function h = sub1()
      h = str2func('sub2');

   function h = sub2()
      h = @(x) sub3(x, 10);

   function q = sub3(a, b)
      q = fprintf('sub3: (a,b) = (%f,%d)\n', a, b);

In Octave I then get


    octave:29> f = foo(), s1 = f(), s2 = s1(), s3 = s2(pi)
    f =


    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


while MATLAB yields the, to me, expected output


    >> f = foo(), s1 = f(), s2 = s1(), s3 = s2(pi)

    f = 


    s1 = 


    s2 = 


    sub3: (a,b) = (3.141593,10)

    s3 =



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;

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 

Best regards,
Bård Skaflestad                            <address@hidden>
SINTEF ICT, Applied mathematics
Forskningsveien 1, No-0314 Oslo
Tel.: (+47) 2206 7642

For the record, I have


    octave:26> ver
    GNU Octave Version 3.2.2
    GNU Octave License: GNU General Public License
    Operating System: Linux #1 SMP Wed Nov 21 14:10:25 EST 
2007 x86_64
    no packages installed.


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


/home/bska/tmp/downloads/octave-3.2.2/configure --enable-shared --enable-dl 
--libdir='/home/bska/system/usr/local/lib64' CXX='g++43' CC='gcc43' 
F77='gfortran43' FC='gfortran43' 
LDFLAGS='-L/home/bska/system/usr/local/lib64 -L/usr/local/lib64 -L/usr/lib64 
-L/lib64' LIBS='-lmetis'


whence 'configure' reports


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

  Source directory:     /home/bska/tmp/downloads/octave-3.2.2
  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 
  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
  Dynamic Linking:                    true (dlopen)
  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
configure:34560: WARNING: GLPK library not found.  The glpk function for 
solving linear programs will be disabled.
configure:34566: WARNING: GraphicsMagick++ config script not found.  Assuming 
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
configure:34701: WARNING: "FLTK config script not found.  Native graphics will 
be disabled."
configure:34717: WARNING: 
configure:34719: WARNING: I didn't find the necessary libraries to compile 
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: 

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


reply via email to

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