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
|
<html lang="en">
<head>
<title>Evaluation - 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="prev" href="Expressions.html#Expressions" title="Expressions">
<link rel="next" href="Statements.html#Statements" title="Statements">
<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="Evaluation"></a>
Next: <a rel="next" accesskey="n" href="Statements.html#Statements">Statements</a>,
Previous: <a rel="previous" accesskey="p" href="Expressions.html#Expressions">Expressions</a>,
Up: <a rel="up" accesskey="u" href="index.html#Top">Top</a>
<hr>
</div>
<h2 class="chapter">9 Evaluation</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>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>eval</code> function lets you do.
<!-- parse.cc -->
<p><a name="doc_002deval"></a>
<div class="defun">
— Built-in Function: <b>eval</b> (<var>try, catch</var>)<var><a name="index-eval-546"></a></var><br>
<blockquote><p>Parse the string <var>try</var> and evaluate it as if it were an Octave
program. If that fails, evaluate the optional string <var>catch</var>.
The string <var>try</var> is evaluated in the current context,
so any results remain available after <code>eval</code> returns.
<p>The following example makes the variable <var>a</var> with the approximate
value 3.1416 available.
<pre class="example"> eval("a = acos(-1);");
</pre>
<p>If an error occurs during the evaluation of <var>try</var> the <var>catch</var>
string is evaluated, as the following example shows:
<pre class="example"> eval ('error ("This is a bad example");',
'printf ("This error occurred:\n%s\n", lasterr ());');
-| This error occurred:
This is a bad example
</pre>
</blockquote></div>
<ul class="menu">
<li><a accesskey="1" href="Calling-a-Function-by-its-Name.html#Calling-a-Function-by-its-Name">Calling a Function by its Name</a>
<li><a accesskey="2" href="Evaluation-in-a-Different-Context.html#Evaluation-in-a-Different-Context">Evaluation in a Different Context</a>
</ul>
</body></html>
|