/usr/share/doc/octave-htmldoc/interpreter/Calling-a-Function-by-its-Name.html is in octave-htmldoc 3.8.2-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU Octave: Calling a Function by its Name</title>
<meta name="description" content="GNU Octave: Calling a Function by its Name">
<meta name="keywords" content="GNU Octave: Calling a Function by its Name">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Evaluation.html#Evaluation" rel="up" title="Evaluation">
<link href="Evaluation-in-a-Different-Context.html#Evaluation-in-a-Different-Context" rel="next" title="Evaluation in a Different Context">
<link href="Evaluation.html#Evaluation" rel="prev" title="Evaluation">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Calling-a-Function-by-its-Name"></a>
<div class="header">
<p>
Next: <a href="Evaluation-in-a-Different-Context.html#Evaluation-in-a-Different-Context" accesskey="n" rel="next">Evaluation in a Different Context</a>, Up: <a href="Evaluation.html#Evaluation" accesskey="u" rel="up">Evaluation</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Calling-a-Function-by-its-Name-1"></a>
<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>
<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.
</p>
<div class="example">
<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></div>
<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 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.
</p>
<a name="XREFfeval"></a><dl>
<dt><a name="index-feval"></a>Built-in Function: <em></em> <strong>feval</strong> <em>(<var>name</var>, …)</em></dt>
<dd><p>Evaluate the function named <var>name</var>. Any arguments after the first
are passed as inputs to the named function. For example,
</p>
<div class="example">
<pre class="example">feval ("acos", -1)
⇒ 3.1416
</pre></div>
<p>calls the function <code>acos</code> with the argument ‘<samp>-1</samp>’.
</p>
<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,
</p>
<div class="example">
<pre class="example"><var>f</var> = @exp;
feval (<var>f</var>, 1)
⇒ 2.7183
<var>f</var> (1)
⇒ 2.7183
</pre></div>
<p>are equivalent ways to call the function referred to by <var>f</var>. If it
cannot be predicted beforehand whether <var>f</var> is a function handle,
function name in a string, or inline function then <code>feval</code> can be used
instead.
</p></dd></dl>
<p>A similar function <code>run</code> exists for calling user script files, that
are not necessarily on the user path
</p>
<a name="XREFrun"></a><dl>
<dt><a name="index-run"></a>Command: <em></em> <strong>run</strong> <em><var>script</var></em></dt>
<dt><a name="index-run-1"></a>Function File: <em></em> <strong>run</strong> <em>("<var>script</var>")</em></dt>
<dd><p>Run <var>script</var> in the current workspace.
</p>
<p>Scripts which reside in directories specified in Octave’s load
path, and which end with the extension <samp>".m"</samp>, can be run simply by
typing their name. For scripts not located on the load path, use <code>run</code>.
</p>
<p>The file name <var>script</var> can be a bare, fully qualified, or relative
filename and with or without a file extension. If no extension is specified,
Octave will first search for a script with the <samp>".m"</samp> extension before
falling back to the script name without an extension.
</p>
<p>Implementation Note: If <var>script</var> includes a path component, then
<code>run</code> first 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.
</p>
<p><strong>See also:</strong> <a href="Manipulating-the-Load-Path.html#XREFpath">path</a>, <a href="Manipulating-the-Load-Path.html#XREFaddpath">addpath</a>, <a href="Script-Files.html#XREFsource">source</a>.
</p></dd></dl>
<hr>
<div class="header">
<p>
Next: <a href="Evaluation-in-a-Different-Context.html#Evaluation-in-a-Different-Context" accesskey="n" rel="next">Evaluation in a Different Context</a>, Up: <a href="Evaluation.html#Evaluation" accesskey="u" rel="up">Evaluation</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|