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
and
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,
ans
holds the most recently computed result, 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 global
declaration
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
the expression
f (x)
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_decl
is nonzero, a
warning is issued for statements like
global a = 1, b
which makes the variables a
and 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_variables
is nonzero, global
variables are initialized to the value of the built-in variable
default_global_variable_value
.
the default value of initialize_global_variables
is zero.
initialize_global_variables
is nonzero, the value of
default_glbaol_variable_value
is 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
the variable default_global_variable_value
is initially undefined.
global x is_global ("x") => 1
?
*
[ 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.
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 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.
-all
-builtins
LOADPATH
.
-functions
-long
-variables
Valid patterns are the same as described for the 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.
Normally also displays if each name is user-defined or builtin;
the -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, i586-pc-linux-gnu
).
DEFAULT_LOADPATH
".:octave-home/lib/version"
.
EDITOR
"emacs"
.
EXEC_PATH
":$PATH"
.
INFO_FILE
"octave-home/info/octave.info"
.
INFO_PROGRAM
"octave-home/libexec/octave/version/exec/arch/info"
.
LOADPATH
":"
, which tells Octave to use the directories
specified by the built-in variable DEFAULT_LOADPATH
.
OCTAVE_HOME
"/usr/local"
.
PAGER
"less", or "more"
.
PS1
"\s:\#> "
.
PS2
"> "
.
PS4
"+ "
.
auto_unload_dot_oct_files
automatic_replot
beep_on_error
completion_append_char
" "
.
default_eval_print_flag
default_return_value
[]
.
default_save_format
"ascii"
.
do_fortran_indexing
crash_dumps_octave_core
define_all_return_values
empty_list_elements_ok
"warn"
.
fixed_point_format
gnuplot_binary
"gnuplot"
.
history_file
"~/.octave_hist"
.
history_size
ignore_function_time_stamp
"system"
.
implicit_num_to_str_ok
implicit_str_to_num_ok
max_recursion_depth
ok_to_lose_imaginary_part
"warn"
.
output_max_field_width
output_precision
page_screen_output
prefer_column_vectors
print_answer_id_name
print_empty_dimensions
resize_on_range_error
return_last_computed_value
save_precision
saving_history
silent_functions
split_long_rows
struct_levels_to_print
suppress_verbose_help_message
treat_neg_dim_as_zero
warn_assign_as_truth_value
if
Statement.
Default value: 1.
warn_comma_in_global_decl
warn_divide_by_zero
warn_function_name_clash
warn_reload_forces_clear
warn_variable_switch_label
switch
Statement.
Default value: 0.
whitespace_in_literal_matrix
""
.
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.
EDITOR
EDITOR
.
OCTAVE_EXEC_PATH
EXEC_PATH
.
Command-line argument: --exec-path
.
OCTAVE_PATH
LOADPATH
.
Command-line argument: --path
.
OCTAVE_INFO_FILE
INFO_FILE
.
Command-line argument: --info-file
.
OCTAVE_INFO_PROGRAM
INFO_PROGRAM
.
Command-line argument: --info-program
.
OCTAVE_HISTSIZE
history_size
.
OCTAVE_HISTFILE
history_file
.
Go to the first, previous, next, last section, table of contents.