<html lang="en"> <head> <title>Calling a Function by its Name - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Evaluation.html#Evaluation" title="Evaluation"> <link rel="next" href="Evaluation-in-a-Different-Context.html#Evaluation-in-a-Different-Context" title="Evaluation in a Different Context"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Calling-a-Function-by-its-Name"></a> <p> Next: <a rel="next" accesskey="n" href="Evaluation-in-a-Different-Context.html#Evaluation-in-a-Different-Context">Evaluation in a Different Context</a>, Up: <a rel="up" accesskey="u" href="Evaluation.html#Evaluation">Evaluation</a> <hr> </div> <h3 class="section">9.1 Calling a Function by its Name</h3> <p>The <code>feval</code> function allows you to call a function from a string containing its name. This is useful when writing a function that needs to call user-supplied functions. The <code>feval</code> function takes the name of the function to call as its first argument, and the remaining arguments are given to the function. <p>The following example is a simple-minded function using <code>feval</code> that finds the root of a user-supplied function of one variable using Newton's method. <pre class="example"> function result = newtroot (fname, x) # usage: newtroot (fname, x) # # fname : a string naming a function f(x). # x : initial guess delta = tol = sqrt (eps); maxit = 200; fx = feval (fname, x); for i = 1:maxit if (abs (fx) < tol) result = x; return; else fx_new = feval (fname, x + delta); deriv = (fx_new - fx) / delta; x = x - fx / deriv; fx = fx_new; endif endfor result = x; endfunction </pre> <p>Note that this is only meant to be an example of calling user-supplied functions and should not be taken too seriously. In addition to using a more robust algorithm, any serious code would check the number and type of all the arguments, ensure that the supplied function really was a function, etc. See <a href="Predicates-for-Numeric-Objects.html#Predicates-for-Numeric-Objects">Predicates for Numeric Objects</a>, for example, for a list of predicates for numeric objects, and see <a href="Status-of-Variables.html#Status-of-Variables">Status of Variables</a>, for a description of the <code>exist</code> function. <!-- feval src/oct-parse.cc --> <p><a name="doc_002dfeval"></a> <div class="defun"> — Built-in Function: <b>feval</b> (<var>name, <small class="dots">...</small></var>)<var><a name="index-feval-682"></a></var><br> <blockquote><p>Evaluate the function named <var>name</var>. Any arguments after the first are passed on to the named function. For example, <pre class="example"> feval ("acos", -1) ⇒ 3.1416 </pre> <p class="noindent">calls the function <code>acos</code> with the argument ‘<samp><span class="samp">-1</span></samp>’. <p>The function <code>feval</code> can also be used with function handles of any sort (see <a href="Function-Handles.html#Function-Handles">Function Handles</a>). Historically, <code>feval</code> was the only way to call user-supplied functions in strings, but function handles are now preferred due to the cleaner syntax they offer. For example, <pre class="example"> <var>f</var> = @exp; feval (<var>f</var>, 1) ⇒ 2.7183 <var>f</var> (1) ⇒ 2.7183 </pre> <p class="noindent">are equivalent ways to call the function referred to by <var>f</var>. If it cannot be predicted beforehand that <var>f</var> is a function handle or the function name in a string, <code>feval</code> can be used instead. </p></blockquote></div> <p>A similar function <code>run</code> exists for calling user script files, that are not necessarily on the user path <!-- run scripts/miscellaneous/run.m --> <p><a name="doc_002drun"></a> <div class="defun"> — Command: <b>run</b><var> script<a name="index-run-683"></a></var><br> — Function File: <b>run</b> (<var>script</var>)<var><a name="index-run-684"></a></var><br> <blockquote><p>Run scripts in the current workspace that are not necessarily on the path. If <var>script</var> is the script to run, including its path, then <code>run</code> changes the directory to the directory where <var>script</var> is found. <code>run</code> then executes the script, and returns to the original directory. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dsystem.html#doc_002dsystem">system</a>. </p></blockquote></div> </body></html>