help-octave
[Top][All Lists]
Advanced

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

Re: Functions as variables


From: Geraint Paul Bevan
Subject: Re: Functions as variables
Date: Thu, 16 Oct 2003 11:08:41 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030624

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Christoph Dalitz wrote:
| Hello,
|
| how can I pass a function as a variable input argument to another
function?
| I have tried the following, but it does not work:
|

Christoph,

you have to pass the function as a string and then use the function
'eval' to evaluate it. The following code works:


function fplot(x,f)
~  y = zeros(1,length(x));
~  for i = 1:length(x)
~    command = ['y(i) = ' ,f ,'(x(i));'];
~    eval(command);
~  endfor
~  plot(x,y);
endfunction


octave:1> fplot([0:0.1:5], 'sin')



- --
Geraint Bevan
http://www.mech.gla.ac.uk/~gbevan

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iEYEARECAAYFAj+ObiYACgkQcXV3N50QmNM22wCfc/kpBP4JxNELa0tudJqTuZRc
RJYAnR9dG8hxfTS1dLKukqCgD9Z0FYUs
=SrCl
-----END PGP SIGNATURE-----



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

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