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; }
28
<a name="Calling-Octave-Functions-from-Oct-Files"></a>
29
<a name="Calling-Octave-Functions-from-Oct_002dFiles"></a>
30
Next: <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: <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: <a rel="up" accesskey="u" href="Oct_002dFiles.html#Oct_002dFiles">Oct-Files</a>
36
<h4 class="subsection">A.1.8 Calling Octave Functions from Oct-Files</h4>
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
44
<p>There are also many ways in which a function might be passed. It might
49
<li>Anonymous Function Handle
54
<p>The example below demonstrates an example that accepts all four means of
55
passing a function to an oct-file.
57
<pre class="example"><pre class="verbatim"> /*
59
Copyright (C) 2006, 2007 John W. Eaton
61
This file is part of Octave.
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
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.
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 <http://www.gnu.org/licenses/>.
80
#include <octave/oct.h>
81
#include <octave/parse.h>
83
DEFUN_DLD (funcdemo, args, nargout, "Function Demo")
85
int nargin = args.length();
86
octave_value_list retval;
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 ())
98
octave_function *fcn = args(0).function_value ();
100
retval = feval (fcn, newargs, nargout);
102
else if (args(0).is_string ())
104
std::string fcn = args (0).string_value ();
106
retval = feval (fcn, newargs, nargout);
109
error ("funcdemo: expected string,",
110
" inline or function handle");
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.
118
<pre class="example"> funcdemo (@sin,1)
120
funcdemo (@(x) sin(x), 1)
122
funcdemo (inline ("sin(x)"), 1)
126
funcdemo (@atan2, 1, 1)
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
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");
141
if (fcn_name.length ())
142
clear_function (fcn_name);
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.