[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: another question : arrays of functions ???
From: |
John W. Eaton |
Subject: |
Re: another question : arrays of functions ??? |
Date: |
Thu, 13 Jun 1996 23:20:06 -0500 |
On 13-Jun-1996, Ted Harding <address@hidden> wrote:
: Octave does not provide for the declaration of arrays of functions in the
: way Pascal (or C) does. You can, however, emulate it by constructing an
: array of function names and using the "eval" function. But you need to
: make sure that you "fudge" the function names to be all the same length in
: the array. The function definitions would be given separately, and
: individually, as in P or C.
:
: For example:
:
: function y = cos_2(x)
: t = cos(x); y = t.*t;
: endfunction
:
: function y = cos_3(x)
: t = cos(x); y = t.*t.*t;
: endfunction
:
: fn = [" cos"; "cos_2"; "cos_3"];
:
: x = [ 0.25 0.5 0.75 ];
: y = [];
: for i = 1: 3, t = eval ([ setstr( fn(i,:) ) "(x);" ]); y=[y;t] endfor
You could simplify this a bit by using feval() instead of eval().
In the next release, the strings won't have be the same length, and
you will also be able to omit the call to setstr():
octave:1> string_fill_char = setstr (0); # Set fill char to ASCII nul.
octave:2> fcns = ["cos"; "acos"] # Lengths need not match.
fcns =
sin
acos
octave:3> for i = 1:2; feval (fcns (i,:), 0), end
ans = 1
ans = 1.5708
jwe