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
|
<html lang="en">
<head>
<title>Calling Octave Functions from Oct-Files - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.11">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Oct_002dFiles.html#Oct_002dFiles" title="Oct-Files">
<link rel="prev" href="Accessing-Global-Variables-in-Oct_002dFiles.html#Accessing-Global-Variables-in-Oct_002dFiles" title="Accessing Global Variables in Oct-Files">
<link rel="next" href="Calling-External-Code-from-Oct_002dFiles.html#Calling-External-Code-from-Oct_002dFiles" title="Calling External Code from Oct-Files">
<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">
<p>
<a name="Calling-Octave-Functions-from-Oct-Files"></a>
<a name="Calling-Octave-Functions-from-Oct_002dFiles"></a>
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>,
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>,
Up: <a rel="up" accesskey="u" href="Oct_002dFiles.html#Oct_002dFiles">Oct-Files</a>
<hr>
</div>
<h4 class="subsection">A.1.8 Calling Octave Functions from Oct-Files</h4>
<p>There is often a need to be able to call another octave function from
within an oct-file, and there are many examples of such within octave
itself. For example the <code>quad</code> function is an oct-file that
calculates the definite integral by quadrature over a user supplied
function.
<p>There are also many ways in which a function might be passed. It might
be passed as one of
<ol type=1 start=1>
<li>Function Handle
<li>Anonymous Function Handle
<li>Inline Function
<li>String
</ol>
<p>The example below demonstrates an example that accepts all four means of
passing a function to an oct-file.
<pre class="example"><pre class="verbatim"> #include <octave/oct.h>
#include <octave/parse.h>
DEFUN_DLD (funcdemo, args, nargout, "Function Demo")
{
int nargin = args.length();
octave_value_list retval;
if (nargin < 2)
print_usage ();
else
{
octave_value_list newargs;
for (octave_idx_type i = nargin - 1; i > 0; i--)
newargs (i - 1) = args(i);
if (args(0).is_function_handle ()
|| args(0).is_inline_function ())
{
octave_function *fcn = args(0).function_value ();
if (! error_state)
retval = feval (fcn, newargs, nargout);
}
else if (args(0).is_string ())
{
std::string fcn = args (0).string_value ();
if (! error_state)
retval = feval (fcn, newargs, nargout);
}
else
error ("funcdemo: expected string,",
" inline or function handle");
}
return retval;
}
</pre></pre>
<p>The first argument to this demonstration is the user supplied function
and the following arguments are all passed to the user function.
<pre class="example"> funcdemo (@sin,1)
0.84147
funcdemo (@(x) sin(x), 1)
0.84147
funcdemo (inline ("sin(x)"), 1)
0.84147
funcdemo ("sin",1)
0.84147
funcdemo (@atan2, 1, 1)
0.78540
</pre>
<p>When the user function is passed as a string, the treatment of the
function is different. In some cases it is necessary to always have the
user supplied function as an <code>octave_function</code> object. In that
case the string argument can be used to create a temporary function like
<pre class="example"> std::octave fcn_name = unique_symbol_name ("__fcn__");
std::string fname = "function y = ";
fname.append (fcn_name);
fname.append ("(x) y = ");
fcn = extract_function (args(0), "funcdemo", fcn_name,
fname, "; endfunction");
...
if (fcn_name.length ())
clear_function (fcn_name);
</pre>
<p>There are two important things to know in this case. The number of input
arguments to the user function is fixed, and in the above is a single
argument, and secondly to avoid leaving the temporary function in the
Octave symbol table it should be cleared after use.
</body></html>
|