Variables let you give names to values and refer to them later. You have already seen variables in many of the examples. The name of a variable must be a sequence of letters, digits and underscores, but it may not begin with a digit. Octave does not enforce a limit on the length of variable names, but it is seldom useful to have variables with names longer than about 30 characters. The following are all valid variable names
x x15 __foo_bar_baz__ fucnrdthsucngtagdjb
However, names like
__foo_bar_baz__ that begin and end with two
underscores are understood to be reserved for internal use by Octave.
You should not use them in code you write, except to access Octave's
documented internal variables and built-in symbolic constants.
Case is significant in variable names. The symbols
A are distinct variables.
A variable name is a valid expression by itself. It represents the variable's current value. Variables are given new values with assignment operators and increment operators. See section Assignment Expressions.
A number of variables have special built-in meanings. For example,
PWD holds the current working directory, and
pi names the
ratio of the circumference of a circle to its diameter. See section Summary of Built-in Variables, for a list of all the predefined variables. Some
of these built-in symbols are constants and may not be changed. Others
can be used and assigned just like all other variables, but their values
are also used or changed automatically by Octave.
Variables in Octave do not have fixed types, so it is possible to first store a numeric value in a variable and then to later use the same name to hold a string value in the same program. Variables may not be used before they have been given a value. Doing so results in an error.
A variable that has been declared global may be accessed from within a function body without having to pass it as a formal parameter.
A variable may be declared global using a
statement. The following statements are all global declarations.
global a global b = 2 global c = 3, d, e = 5
It is necessary declare a variable as global within a function body in order to access it. For example,
global x function f () x = 1; endfunction f ()
does not set the value of the global variable
x to 1. In
order to change the value of the global variable
x, you must also
declare it to be global within the function body, like this
function f () global x; x = 1; endfunction
Passing a global variable in a function parameter list will make a local copy and not modify the global value. For example, given the function
function f (x) x = 0 endfunction
and the definition of
x as a global variable at the top level,
global x = 13
will display the value of
x from inside the function as 0,
but the value of
x at the top level remains unchanged, because
the function works with a copy of its argument.
warn_comma_in_global_declis nonzero, a warning is issued for statements like
global a = 1, b
which makes the variables
b global and assigns the
value 1 to the variable
a, because in this context, the comma is
not interpreted as a statement separator.
The default value of
warn_comma_in_global_decl is nonzero.
initialize_global_variablesis nonzero, global variables are initialized to the value of the built-in variable
the default value of
initialize_global_variables is zero.
initialize_global_variablesis nonzero, the value of
default_glbaol_variable_valueis used as the initial value of global variables that are not explicitly initialized. for example,
initialize_global_variables = 1; default_global_variable_value = 13; global foo; foo => 13
default_global_variable_value is initially undefined.
global x is_global ("x") => 1
[ list ]
^, match all characters except those specified by list. For example, the pattern `[a-zA-Z]' will match all lower and upper case alphabetic characters.
For example, the command
clear foo b*r
clears the name
foo and all names that begin with the letter
b and end with the letter
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
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.
Valid patterns are the same as described for the
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.
Normally also displays if each name is user-defined or builtin;
-q option suppresses this behaviour.
Currently, Octave can only display functions that can be compiled cleanly, because it uses its internal representation of the function to recreate the program text.
Comments are not displayed because Octave's parser currently discards them as it converts the text of a function file to its internal representation. This problem may be fixed in a future release.
Here is a summary of all of Octave's built-in variables along with
cross references to additional information and their default values. In
the following table octave-home stands for the root directory
where all of Octave is installed (the default is `/usr/local',
version stands for the Octave version number (for example,
2.0.13) and arch stands for the type of system for which
Octave was compiled (for example,
"less", or "more".
ifStatement. Default value: 1.
switchStatement. Default value: 0.
Octave uses the values of the following environment variables to set the default values for the corresponding built-in variables. In addition, the values from the environment may be overridden by command-line arguments. See section Command Line Options.
EXEC_PATH. Command-line argument:
LOADPATH. Command-line argument:
INFO_FILE. Command-line argument:
INFO_PROGRAM. Command-line argument:
Go to the first, previous, next, last section, table of contents.