File: Calling-a-Function-by-its-Name.html

package info (click to toggle)
octave 3.6.2-5%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 71,636 kB
  • sloc: cpp: 241,186; fortran: 23,651; sh: 14,790; ansic: 7,153; lex: 3,761; objc: 3,404; yacc: 3,386; makefile: 2,073; awk: 985; perl: 838
file content (130 lines) | stat: -rw-r--r-- 5,738 bytes parent folder | download
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
<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:&nbsp;<a rel="next" accesskey="n" href="Evaluation-in-a-Different-Context.html#Evaluation-in-a-Different-Context">Evaluation in a Different Context</a>,
Up:&nbsp;<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) &lt; 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">
&mdash; 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)
               &rArr; 3.1416
</pre>
        <p class="noindent">calls the function <code>acos</code> with the argument &lsquo;<samp><span class="samp">-1</span></samp>&rsquo;.

        <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)
               &rArr; 2.7183
          <var>f</var> (1)
               &rArr; 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">
&mdash; Command:  <b>run</b><var> script<a name="index-run-683"></a></var><br>
&mdash; 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>