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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Evaluation (GNU Octave (version 10.3.0))</title>
<meta name="description" content="Evaluation (GNU Octave (version 10.3.0))">
<meta name="keywords" content="Evaluation (GNU Octave (version 10.3.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="index.html" rel="up" title="Top">
<link href="Statements.html" rel="next" title="Statements">
<link href="Expressions.html" rel="prev" title="Expressions">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">
</head>
<body lang="en">
<div class="chapter-level-extent" id="Evaluation">
<div class="nav-panel">
<p>
Next: <a href="Statements.html" accesskey="n" rel="next">Statements</a>, Previous: <a href="Expressions.html" accesskey="p" rel="prev">Expressions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h2 class="chapter" id="Evaluation-1"><span>9 Evaluation<a class="copiable-link" href="#Evaluation-1"> ¶</a></span></h2>
<p>Normally, you evaluate expressions simply by typing them at the Octave
prompt, or by asking Octave to interpret commands that you have saved in
a file.
</p>
<p>Sometimes, you may find it necessary to evaluate an expression that has
been computed and stored in a string, which is exactly what the
<code class="code">eval</code> function lets you do.
</p>
<a class="anchor" id="XREFeval"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-eval"><span><strong class="def-name">eval</strong> <code class="def-code-arguments">(<var class="var">try</var>)</code><a class="copiable-link" href="#index-eval"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-eval-1"><span><strong class="def-name">eval</strong> <code class="def-code-arguments">(<var class="var">try</var>, <var class="var">catch</var>)</code><a class="copiable-link" href="#index-eval-1"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-eval-2"><span><code class="def-type">[<var class="var">var1</var>, …] =</code> <strong class="def-name">eval</strong> <code class="def-code-arguments">(…)</code><a class="copiable-link" href="#index-eval-2"> ¶</a></span></dt>
<dd><p>Parse the string <var class="var">try</var> and evaluate it as if it were an Octave program.
</p>
<p>If execution fails, evaluate the optional string <var class="var">catch</var>.
</p>
<p>The string <var class="var">try</var> is evaluated in the current context, so any results remain
available after <code class="code">eval</code> returns.
</p>
<p>The following example creates the variable <var class="var">A</var> with the approximate value
of pi (3.1416) in the current workspace.
</p>
<div class="example">
<pre class="example-preformatted">eval ('A = acos (-1);');
</pre></div>
<p>If an error occurs during the evaluation of <var class="var">try</var> then the <var class="var">catch</var>
string is evaluated, as the following example shows:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">eval ('error ("This is a bad example");',
'printf ("This error occurred:\n%s\n", lasterr ());');
-| This error occurred:
This is a bad example
</pre></div></div>
<p>Rather than create variables as part of the code string <var class="var">try</var>, it is
clearer and slightly faster to assign the results of evaluation to an output
variable(s). The first example can be re-written as
</p>
<div class="example">
<pre class="example-preformatted">A = eval ('acos (-1);');
</pre></div>
<p>Programming Note: if you are only using <code class="code">eval</code> as an error-capturing
mechanism, rather than for the execution of arbitrary code strings, consider
using <code class="code">try</code>/<code class="code">catch</code> blocks or
<code class="code">unwind_protect</code>/<code class="code">unwind_protect_cleanup</code> blocks instead. These
techniques have higher performance and don’t introduce the security
considerations that the evaluation of arbitrary code does.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="Evaluation-in-a-Different-Context.html#XREFevalin">evalin</a>, <a class="ref" href="#XREFevalc">evalc</a>, <a class="ref" href="Evaluation-in-a-Different-Context.html#XREFassignin">assignin</a>, <a class="ref" href="Calling-a-Function-by-its-Name.html#XREFfeval">feval</a>, <a class="ref" href="Keywords.html#XREFtry">try</a>, <a class="ref" href="Keywords.html#XREFunwind_005fprotect">unwind_protect</a>.
</p></dd></dl>
<p>The <code class="code">evalc</code> function additionally captures any console output
produced by the evaluated expression.
</p>
<a class="anchor" id="XREFevalc"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-evalc"><span><code class="def-type"><var class="var">s</var> =</code> <strong class="def-name">evalc</strong> <code class="def-code-arguments">(<var class="var">try</var>)</code><a class="copiable-link" href="#index-evalc"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-evalc-1"><span><code class="def-type"><var class="var">s</var> =</code> <strong class="def-name">evalc</strong> <code class="def-code-arguments">(<var class="var">try</var>, <var class="var">catch</var>)</code><a class="copiable-link" href="#index-evalc-1"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-evalc-2"><span><code class="def-type">[~, <var class="var">var1</var>, …] =</code> <strong class="def-name">evalc</strong> <code class="def-code-arguments">(…)</code><a class="copiable-link" href="#index-evalc-2"> ¶</a></span></dt>
<dd><p>Parse and evaluate the string <var class="var">try</var> as if it were an Octave program,
while capturing the output into the return variable <var class="var">s</var>.
</p>
<p>If execution fails, evaluate the optional string <var class="var">catch</var>.
</p>
<p>This function behaves like <code class="code">eval</code>, but any output or warning messages
which would normally be written to the console are captured and returned in
the string <var class="var">s</var>.
</p>
<p>If the first output <var class="var">s</var> is ignored with <code class="code">~</code> then the actual results
of the code evaluation (not the string capture) will be assigned to output
variables <var class="var">var1</var>, <var class="var">var2</var>, etc.
</p>
<p>Example 1:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">s = evalc ("t = 42"), t
⇒ s = t = 42
⇒ t = 42
</pre></div></div>
<p>Example 2:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">[~, p] = evalc ("pi")
⇒ p = 3.1416
</pre></div></div>
<p>Programming Note: The <code class="code">diary</code> is disabled during the execution of this
function. When <code class="code">system</code> is used, any output produced by external programs
is <em class="emph">not</em> captured, unless their output is captured by the <code class="code">system</code>
function itself.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFeval">eval</a>, <a class="ref" href="Diary-and-Echo-Commands.html#XREFdiary">diary</a>.
</p></dd></dl>
<ul class="mini-toc">
<li><a href="Calling-a-Function-by-its-Name.html" accesskey="1">Calling a Function by its Name</a></li>
<li><a href="Evaluation-in-a-Different-Context.html" accesskey="2">Evaluation in a Different Context</a></li>
</ul>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Statements.html">Statements</a>, Previous: <a href="Expressions.html">Expressions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|