~ubuntu-branches/debian/sid/octave3.0/sid

« back to all changes in this revision

Viewing changes to doc/interpreter/HTML/Calling-Octave-Functions-from-Oct_002dFiles.html

  • Committer: Bazaar Package Importer
  • Author(s): Rafael Laboissiere
  • Date: 2007-12-23 16:04:15 UTC
  • Revision ID: james.westby@ubuntu.com-20071223160415-n4gk468dihy22e9v
Tags: upstream-3.0.0
ImportĀ upstreamĀ versionĀ 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<html lang="en">
 
2
<head>
 
3
<title>Calling Octave Functions from Oct-Files - Untitled</title>
 
4
<meta http-equiv="Content-Type" content="text/html">
 
5
<meta name="description" content="Untitled">
 
6
<meta name="generator" content="makeinfo 4.11">
 
7
<link title="Top" rel="start" href="index.html#Top">
 
8
<link rel="up" href="Oct_002dFiles.html#Oct_002dFiles" title="Oct-Files">
 
9
<link rel="prev" href="Accessing-Global-Variables-in-Oct_002dFiles.html#Accessing-Global-Variables-in-Oct_002dFiles" title="Accessing Global Variables in Oct-Files">
 
10
<link rel="next" href="Calling-External-Code-from-Oct_002dFiles.html#Calling-External-Code-from-Oct_002dFiles" title="Calling External Code from Oct-Files">
 
11
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
 
12
<meta http-equiv="Content-Style-Type" content="text/css">
 
13
<style type="text/css"><!--
 
14
  pre.display { font-family:inherit }
 
15
  pre.format  { font-family:inherit }
 
16
  pre.smalldisplay { font-family:inherit; font-size:smaller }
 
17
  pre.smallformat  { font-family:inherit; font-size:smaller }
 
18
  pre.smallexample { font-size:smaller }
 
19
  pre.smalllisp    { font-size:smaller }
 
20
  span.sc    { font-variant:small-caps }
 
21
  span.roman { font-family:serif; font-weight:normal; } 
 
22
  span.sansserif { font-family:sans-serif; font-weight:normal; } 
 
23
--></style>
 
24
</head>
 
25
<body>
 
26
<div class="node">
 
27
<p>
 
28
<a name="Calling-Octave-Functions-from-Oct-Files"></a>
 
29
<a name="Calling-Octave-Functions-from-Oct_002dFiles"></a>
 
30
Next:&nbsp;<a rel="next" accesskey="n" href="Calling-External-Code-from-Oct_002dFiles.html#Calling-External-Code-from-Oct_002dFiles">Calling External Code from Oct-Files</a>,
 
31
Previous:&nbsp;<a rel="previous" accesskey="p" href="Accessing-Global-Variables-in-Oct_002dFiles.html#Accessing-Global-Variables-in-Oct_002dFiles">Accessing Global Variables in Oct-Files</a>,
 
32
Up:&nbsp;<a rel="up" accesskey="u" href="Oct_002dFiles.html#Oct_002dFiles">Oct-Files</a>
 
33
<hr>
 
34
</div>
 
35
 
 
36
<h4 class="subsection">A.1.8 Calling Octave Functions from Oct-Files</h4>
 
37
 
 
38
<p>There is often a need to be able to call another octave function from
 
39
within an oct-file, and there are many examples of such within octave
 
40
itself.  For example the <code>quad</code> function is an oct-file that
 
41
calculates the definite integral by quadrature over a user supplied
 
42
function.
 
43
 
 
44
   <p>There are also many ways in which a function might be passed.  It might
 
45
be passed as one of
 
46
 
 
47
     <ol type=1 start=1>
 
48
<li>Function Handle
 
49
<li>Anonymous Function Handle
 
50
<li>Inline Function
 
51
<li>String
 
52
        </ol>
 
53
 
 
54
   <p>The example below demonstrates an example that accepts all four means of
 
55
passing a function to an oct-file.
 
56
 
 
57
<pre class="example"><pre class="verbatim">     /*
 
58
     
 
59
     Copyright (C) 2006, 2007 John W. Eaton
 
60
     
 
61
     This file is part of Octave.
 
62
     
 
63
     Octave is free software; you can redistribute it and/or 
 
64
     modify it under the terms of the GNU General Public License 
 
65
     as published by the Free Software Foundation; either
 
66
     version 3  of the License, or (at your option) any later 
 
67
     version.
 
68
     
 
69
     Octave is distributed in the hope that it will be useful, 
 
70
     but WITHOUT ANY WARRANTY; without even the implied warranty
 
71
     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 
72
     See the GNU General Public License for more details.
 
73
     
 
74
     You should have received a copy of the GNU General Public 
 
75
     License along with Octave; see the file COPYING.  If not,
 
76
     see &lt;http://www.gnu.org/licenses/>.
 
77
     
 
78
     */
 
79
     
 
80
     #include &lt;octave/oct.h>
 
81
     #include &lt;octave/parse.h>
 
82
     
 
83
     DEFUN_DLD (funcdemo, args, nargout, "Function Demo")
 
84
     {
 
85
       int nargin = args.length();
 
86
       octave_value_list retval;
 
87
     
 
88
       if (nargin &lt; 2)
 
89
         print_usage ();
 
90
       else
 
91
         {
 
92
           octave_value_list newargs;
 
93
           for (octave_idx_type i = nargin - 1; i > 0; i--)
 
94
             newargs (i - 1) = args(i);
 
95
           if (args(0).is_function_handle ()
 
96
               || args(0).is_inline_function ())
 
97
             {
 
98
               octave_function *fcn = args(0).function_value ();
 
99
               if (! error_state)
 
100
                 retval = feval (fcn, newargs, nargout);
 
101
             }
 
102
           else if (args(0).is_string ())
 
103
             {
 
104
               std::string fcn = args (0).string_value ();
 
105
               if (! error_state)
 
106
                 retval = feval (fcn, newargs, nargout);
 
107
             }
 
108
           else
 
109
             error ("funcdemo: expected string,",
 
110
               " inline or function handle");
 
111
         }
 
112
       return retval;
 
113
     }
 
114
</pre></pre>
 
115
   <p>The first argument to this demonstration is the user supplied function
 
116
and the following arguments are all passed to the user function.
 
117
 
 
118
<pre class="example">     funcdemo (@sin,1)
 
119
      0.84147
 
120
     funcdemo (@(x) sin(x), 1)
 
121
      0.84147
 
122
     funcdemo (inline ("sin(x)"), 1)
 
123
      0.84147
 
124
     funcdemo ("sin",1)
 
125
      0.84147
 
126
     funcdemo (@atan2, 1, 1)
 
127
      0.78540
 
128
</pre>
 
129
   <p>When the user function is passed as a string, the treatment of the
 
130
function is different.  In some cases it is necessary to always have the
 
131
user supplied function as an <code>octave_function</code> object.  In that
 
132
case the string argument can be used to create a temporary function like
 
133
 
 
134
<pre class="example">     std::octave fcn_name = unique_symbol_name ("__fcn__");
 
135
     std::string fname = "function y = ";
 
136
     fname.append (fcn_name);
 
137
     fname.append ("(x) y = ");
 
138
     fcn = extract_function (args(0), "funcdemo", fcn_name,
 
139
                             fname, "; endfunction");
 
140
     ...
 
141
     if (fcn_name.length ())
 
142
       clear_function (fcn_name);
 
143
</pre>
 
144
   <p>There are two important things to know in this case.  The number of input
 
145
arguments to the user function is fixed, and in the above is a single
 
146
argument, and secondly to avoid leaving the temporary function in the
 
147
Octave symbol table it should be cleared after use.
 
148
 
 
149
   </body></html>
 
150