Contents Previous Next Subchapters

Evaluating a Character Matrix
Syntax eval(cmd)
See Also execfile

Description
Compiles and executes the O-Matrix command contained in the character row vector cmd.

Simple Example
If you enter
     x = 5
     eval("x = x + x")
     print x
O-Matrix will respond
     10

Returning A value
The eval function will return a value when cmd is an expression. If you enter
     y = eval("1 + 2")
     print y
O-Matrix will respond
     3

Multiple Return Values
If the cmd is a call to a function that returns multiple values, the eval function will return those values. If you enter
     clear
     function [x, y] = f(a, b) begin
          x = 2 * a
          y = 2 * b
     end
     [u, v] = eval("f(1, 2)")
     print u, v
O-Matrix will respond
     2, 4 

Variable Scoping
The eval function references variables as if they were in the currently executing file and function. If you enter
     x = 1
     function f() begin
          x = 3
          eval("x = x + x")
          print x
     end
     f
O-Matrix will respond
     6
when it executes the function f. If you continue by entering
     x
O-Matrix will respond
     1
because the variable x in the main program is different from the variable x with in the function f.

Creating New Variables
If a variable is created by a call to the eval function it cannot be used until after the call is executed. If you enter
     clear
     begin
          eval("a = 1")
          eval("print a")
     end
O-Matrix will respond
     1
If on the other hand you enter
     clear
     begin
          eval("a = 1")
          print a
     end
O-Matrix will respond with an error message saying that the variable a is not yet defined at the print statement. This is because the call to the eval function has not yet been executed at the time when the print statement is compiled.

Restrictions
If there is more than one command in a call to eval only the first command is executed. If you enter
     eval("print 1; print 2")
O-Matrix will respond
     1