function handle changes

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

function handle changes

John W. Eaton-6
I checked in some changes to CVS that add Matlab-compatible function
handles to Octave.  This currently only works for scalar function
handles (you can't make a collection of function handles, except by
using a cell array).

One advantage of using function handles over passing the name of a
function as a string is that you can pass a handle to a subfunction to
feval (or any other function that uses feval).  For example:

  =====
  fun.m
  =====

  function fun ()

    puts ("\nfun: feval (@sin, pi)\n");
    feval (@sin, pi)

    puts ("\nfun: fun2 (@sin, pi)\n");
    fun2 (@sin, pi)

    puts ("fun: functions (@fun)\n");
    functions (@fun)

    puts ("fun: functions (@fun2)\n");
    functions (@fun2)

    puts ("fun: functions (@sin)\n");
    functions (@sin)

  function sin (val)

    puts ("sin: foo-i-hithere\n");


  ======
  fun2.m
  ======

  function fun2 (f, val)

    puts ("fun2: feval (f, val)\n");
    feval (f, val)

    puts ("\nfun2: functions (f)\n");
    functions (f)

    puts ("fun2: feval (@sin, val)\n");
    feval (@sin, val)

    puts ("\nfun2: functions (@sin)\n");
    functions (@sin)


  octave:1> fun

  fun: feval (@sin, pi)
  sin: foo-i-hithere

  fun: fun2 (@sin, pi)
  fun2: feval (f, val)
  sin: foo-i-hithere

  fun2: functions (f)
  ans =
  {
    file = /scratch/jwe/build/octave-static/fun.m
    function = sin
    type = subfunction
  }

  fun2: feval (@sin, val)
  ans =  1.2246e-16

  fun2: functions (@sin)
  ans =
  {
    file = built-in function
    function = sin
    type = simple
  }

  fun: functions (@fun)
  ans =
  {
    file = /scratch/jwe/build/octave-static/fun.m
    function = fun
    type = simple
  }

  fun: functions (@fun2)
  ans =
  {
    file = /scratch/jwe/build/octave-static/fun2.m
    function = fun2
    type = simple
  }

  fun: functions (@sin)
  ans =
  {
    file = /scratch/jwe/build/octave-static/fun.m
    function = sin
    type = subfunction
  }



Allowing for arrays of function handles will probably happen sometime
after we decide how best to add multidimensional arrays to Octave.

jwe