EXECUTE Function

Compiles and executes one or more PV‑WAVE statements contained in a string at run-time.

Usage

    result = EXECUTE(string)

Input Parameters

string—A string containing the PV‑WAVE command(s) to be compiled and executed. Cannot contain a command that starts with either a dollar ($), period (.), or at (@) character; such commands must be entered at the PV‑WAVE prompt.

Returned Value

result—Returns 1 if the string was successfully compiled and executed; returns 0 if an error occurs during either phase.

Keywords

None.

Discussion

When the EXECUTE function is used inside a procedure or function, the compiler directive ..LOCALS can be used to pre-allocate memory for local variables created at run time (see the PV‑WAVE Programmer’s Guide).

Example

This example creates a procedure, TABLE, that prints a table giving the results of evaluating a user-defined function of two variables at the values in two vectors. A user-defined printing procedure is used to actually display the table of values.

Function EXECUTE is used to invoke both the user-defined function of two variables and the user-defined printing procedure. The name of the function is passed to TABLE using keyword Func, and the name of the printing procedure is passed using keyword Prt_Pro. The following is a listing of TABLE:

PRO TABLE, x, y, Func=func, Prt_Pro=pp
   tab = FLTARR(3, n_elements(x))
   tab(0, *) = x
   tab(1, *) = y
   ; Use EXECUTE to invoke the function in the string 
   ; variable func. Assign the result to column 2 of tab.
   val = EXECUTE('tab(2, *) = ' + func + '(tab(0, *), tab(1, *))')
   ; Use EXECUTE to invoke procedure in the string variable pp.
   ; This procedure prints the table.
   IF val EQ 1 THEN BEGIN
      val = EXECUTE(pp + ', tab')
   IF val EQ 0 THEN BEGIN
      PRINT, "***Error in execution of printing procedure! ***"
   ENDIF
   ENDIF ELSE BEGIN
      PRINT, "*** Error in execution of function! ***"
   ENDELSE
END

If this procedure is placed in the file table.pro in your working directory, it will be compiled automatically when it is invoked. Note that the string concatenation operator, along with several string literals, are used to construct the statements to execute using EXECUTE.

The user-defined function requires two arguments, which are the values of the independent variables of the function. The function should return the result of the function evaluation. The user-defined printing procedure requires one argument, which is the two-dimensional table to be printed. The following commands can be entered at the interactive prompt to create and compile a function of two variables:

.RUN
- FUNCTION fcn, x, y
- RETURN, x^2 - y^2
- END

The following procedure prints the table:

PRO prt, arr
   PRINT, Format = '(4x, "x", 13x, "y", 10x, "func(x, y)")'
   PRINT, Format = '(39("-"))'
   PRINT, Format = '(2(f9.4, 5x), f10.4)', arr
END

If this procedure is placed in the file prt.pro in your working directory, it will be compiled automatically when it is invoked. The following commands can be used to create the vectors of values at which to evaluate fcn and to invoke TABLE:

x = [1, 2, 3, 4, 5]
y = REVERSE(x)
TABLE, x, y, Func = 'fcn', Prt_pro = 'prt'

PV‑WAVE prints the following:

    x            y           func(x, y)
---------------------------------------
 1.0000        5.0000        -24.0000
 2.0000        4.0000        -12.0000
 3.0000        3.0000          0.0000
 4.0000        2.0000         12.0000
 5.0000        1.0000         24.0000

See Also

For more information, see the PV‑WAVE Programmer’s Guide.