## 9 Evaluation ¶

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.

Sometimes, you may find it necessary to evaluate an expression that has been computed and stored in a string, which is exactly what the `eval` function lets you do.

eval `(try)` ¶
eval `(try, catch)` ¶
`[var1, …] =` eval `(…)` ¶

  Parse the string try and evaluate it as if it were an Octave program.

  If execution fails, evaluate the optional string catch.

  The string try is evaluated in the current context, so any results remain available after `eval` returns.

  The following example creates the variable A with the approximate value of pi (3.1416) in the current workspace.

      eval ('A = acos (-1);');

  If an error occurs during the evaluation of try then the catch string is evaluated, as the following example shows:

      eval ('error ("This is a bad example");',
            'printf ("This error occurred:\n%s\n", lasterr ());');
           -| This error occurred:
              This is a bad example

  Rather than create variables as part of the code string try, 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

      A = eval ('acos (-1);');

  Programming Note: if you are only using `eval` as an error-capturing mechanism, rather than for the execution of arbitrary code strings, consider using `try`/`catch` blocks or `unwind_protect`/`unwind_protect_cleanup` blocks instead. These techniques have higher performance and don’t introduce the security considerations that the evaluation of arbitrary code does.

  See also: evalin, evalc, assignin, feval, try, unwind_protect.

The `evalc` function additionally captures any console output produced by the evaluated expression.

`s =` evalc `(try)` ¶
`s =` evalc `(try, catch)` ¶
`[~, var1, …] =` evalc `(…)` ¶

  Parse and evaluate the string try as if it were an Octave program, while capturing the output into the return variable s.

  If execution fails, evaluate the optional string catch.

  This function behaves like `eval`, but any output or warning messages which would normally be written to the console are captured and returned in the string s.

  If the first output s is ignored with `~` then the actual results of the code evaluation (not the string capture) will be assigned to output variables var1, var2, etc.

  Example 1:

      s = evalc ("t = 42"), t
        ⇒  s = t =  42

        ⇒  t =  42

  Example 2:

      [~, p] = evalc ("pi")
        ⇒  p = 3.1416

  Programming Note: The `diary` is disabled during the execution of this function. When `system` is used, any output produced by external programs is not captured, unless their output is captured by the `system` function itself.

  See also: eval, diary.