<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.13"> <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"> <a name="Calling-Octave-Functions-from-Oct-Files"></a> <a name="Calling-Octave-Functions-from-Oct_002dFiles"></a> <p> 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>