It is often useful to evaluate a string as if it were an Octave program, or use a string as the name of a function to call. These functions are necessary in order to evaluate commands that are not known until run time, or to write functions that will need to call user-supplied functions.
The function eval (command)
parses command and
evaluates it as if it were an Octave program, returning the last value
computed. The command is evaluated in the current context, so any
results remain available after eval
returns. For example,
octave:13> a error: `a' undefined octave:14> eval ("a = 13") a = 13 ans = 13 octave:15> a a = 13
In this case, two values are printed: one for the expression that was
evaluated, and one for the value returned from eval
. Just as
with any other expression, you can turn printing off by ending the
expression in a semicolon. For example,
octave:13> a error: `a' undefined octave:14> eval ("a = 13;") ans = 13 octave:15> a a = 13
The function feval (name, ...)
can be used to evaluate
the function named name. Any arguments after the first are passed
on to the named function. For example,
octave:12> feval ("acos", -1) ans = 3.1416
calls the function acos
with the argument `-1'.
The function feval
is necessary in order to be able to write
functions that call user-supplied functions, because Octave does not
have a way to declare a pointer to a function (like C) or to declare a
special kind of variable that can be used to hold the name of a function
(like EXTERNAL
in Fortran). Instead, you must refer to functions
by name, and use feval
to call them.
For example, here is a simple-minded function for finding the root of a function of one variable:
function result = newtroot (fname, x) # usage: newtroot (fname, x) # # fname : a string naming a function f(x). # x : initial guess delta = tol = sqrt (eps); maxit = 200; fx = feval (fname, x); for i = 1:maxit if (abs (fx) < tol) result = x; return; else fx_new = feval (fname, x + delta); deriv = (fx_new - fx) / delta; x = x - fx / deriv; fx = fx_new; endif endfor result = x; endfunction
Note that this is only meant to be an example of calling user-supplied functions and should not be taken too seriously. In addition to using a more robust algorithm, any serious code would check the number and type of all the arguments, ensure that the supplied function really was a function, etc.
The following functions allow you to determine the size of a variable or expression, find out whether a variable exists, print error messages, or delete variable names from the symbol table.
columns (a)
rows (a)
length (a)
size (a [, n])
octave:13> size ([1, 2; 3, 4; 5, 6]) ans = 3 2 octave:14> [nr, nc] = size ([1, 2; 3, 4; 5, 6]) nr = 3 nc = 2If given a second argument of either 1 or 2,
size
will return
only the row or column dimension. For example
octave:15> size ([1, 2; 3, 4; 5, 6], 2) ans = 2returns the number of columns in the given matrix.
is_global (a)
is_matrix (a)
is_vector (a)
is_scalar (a)
is_square (x)
is_symmetric (x, tol)
isstr (a)
isempty (a)
clear pattern ...
?
*
[ list ]
!
or ^
, match all characters except those
specified by list. For example, the pattern `[a-zA-Z]' will
match all lower and upper case alphabetic characters.
clear foo b*rclears the name
foo
and all names that begin with the letter
b
and end with the letter r
.
If clear
is called without any arguments, all user-defined
variables (local and global) are cleared from the symbol table. If
clear
is called with at least one argument, only the visible
names matching the arguments are cleared. For example, suppose you have
defined a function foo
, and then hidden it by performing the
assignment foo = 2
. Executing the command `clear foo' once
will clear the variable definition and restore the definition of
foo
as a function. Executing `clear foo' a second time will
clear the function definition.
This command may not be used within a function body.
who options pattern ...
-all
-builtins
LOADPATH
.
-functions
-long
-variables
clear
command
above. If no patterns are supplied, all symbols from the given category
are listed. By default, only user defined functions and variables
visible in the local scope are displayed.
The command whos
is equivalent to who -long
.
exist (name)
error (msg)
function f () g () end function g () h () end function h () nargin == 1 || error ("nargin != 1"); end f () error: nargin != 1 error: evaluating index expression near line 1, column 30 error: evaluating binary operator `||' near line 1, column 27 error: called from `h' error: called from `g' error: called from `f'produces a list of messages that can help you to quickly locate the exact location of the error. If msg ends in a new line character, Octave will only print msg and will not display any traceback messages as it returns control to the top level. For example, modifying the error message in the previous example to end in a new line causes Octave to only print a single message:
function h () nargin == 1 || error ("nargin != 1\n"); end f () error: nargin != 1
warning (msg)
usage (msg)
usage
is evaluated, Octave will print a traceback of all
the function calls leading to the usage message.
perror (name, num)
menu (title, opt1, ...)
document symbol text
file_in_path (path, file)
LOADPATH
.
If the file cannot be found in the path, an empty matrix is returned.
For example,
octave:13> file_in_path (LOADPATH, "nargchk.m") ans = /usr/local/lib/octave/1.1.0/m/general/nargchk.m
nargchk (nargin_min, nargin_max, n)
octave_tmp_file_name
octave_tmp_file_name
, it
is possible (though relatively unlikely) that it will not be available
by the time your program attempts to open it.
type name ...
which name ...