2679
|
1 @c Copyright (C) 1996, 1997 John W. Eaton |
|
2 @c This is part of the Octave manual. |
|
3 @c For copying conditions, see the file gpl.texi. |
|
4 |
|
5 @node Variables, Expressions, Data Structures, Top |
|
6 @chapter Variables |
|
7 @cindex variables, user-defined |
|
8 @cindex user-defined variables |
|
9 |
|
10 Variables let you give names to values and refer to them later. You have |
|
11 already seen variables in many of the examples. The name of a variable |
|
12 must be a sequence of letters, digits and underscores, but it may not begin |
|
13 with a digit. Octave does not enforce a limit on the length of variable |
|
14 names, but it is seldom useful to have variables with names longer than |
|
15 about 30 characters. The following are all valid variable names |
|
16 |
|
17 @cindex job hunting |
|
18 @cindex getting a good job |
|
19 @cindex flying high and fast |
|
20 @example |
|
21 @group |
|
22 x |
|
23 x15 |
|
24 __foo_bar_baz__ |
|
25 fucnrdthsucngtagdjb |
|
26 @end group |
|
27 @end example |
|
28 |
|
29 @noindent |
|
30 However, names like @code{__foo_bar_baz__} that begin and end with two |
|
31 underscores are understood to be reserved for internal use by Octave. |
|
32 You should not use them in code you write, except to access Octave's |
|
33 documented internal variables and built-in symbolic constants. |
|
34 |
|
35 Case is significant in variable names. The symbols @code{a} and |
|
36 @code{A} are distinct variables. |
|
37 |
|
38 A variable name is a valid expression by itself. It represents the |
|
39 variable's current value. Variables are given new values with |
|
40 @dfn{assignment operators} and @dfn{increment operators}. |
|
41 @xref{Assignment Ops, ,Assignment Expressions}. |
|
42 |
|
43 A number of variables have special built-in meanings. For example, |
|
44 @code{PWD} holds the current working directory, and @code{pi} names the |
|
45 ratio of the circumference of a circle to its diameter. @xref{Summary of |
|
46 Built-in Variables}, for a list of all the predefined variables. Some |
|
47 of these built-in symbols are constants and may not be changed. Others |
|
48 can be used and assigned just like all other variables, but their values |
|
49 are also used or changed automatically by Octave. |
|
50 |
|
51 Variables in Octave do not have fixed types, so it is possible to first |
|
52 store a numeric value in a variable and then to later use the same name |
|
53 to hold a string value in the same program. Variables may not be used |
|
54 before they have been given a value. Doing so results in an error. |
|
55 |
|
56 @menu |
|
57 * Global Variables:: |
|
58 * Status of Variables:: |
|
59 * Summary of Built-in Variables:: |
|
60 * Defaults from the Environment:: |
|
61 @end menu |
|
62 |
|
63 @node Global Variables, Status of Variables, Variables, Variables |
|
64 @section Global Variables |
|
65 @cindex global variables |
|
66 @cindex @code{global} statement |
|
67 @cindex variables, global |
|
68 |
|
69 A variable that has been declared @dfn{global} may be accessed from |
|
70 within a function body without having to pass it as a formal parameter. |
|
71 |
|
72 A variable may be declared global using a @code{global} declaration |
|
73 statement. The following statements are all global declarations. |
|
74 |
|
75 @example |
|
76 @group |
|
77 global a |
|
78 global b = 2 |
|
79 global c = 3, d, e = 5 |
|
80 @end group |
|
81 @end example |
|
82 |
|
83 It is necessary declare a variable as global within a function body in |
|
84 order to access it. For example, |
|
85 |
|
86 @example |
|
87 @group |
|
88 global x |
|
89 function f () |
2689
|
90 x = 1; |
2679
|
91 endfunction |
|
92 f () |
|
93 @end group |
|
94 @end example |
|
95 |
|
96 @noindent |
2689
|
97 does @emph{not} set the value of the global variable @code{x} to 1. In |
|
98 order to change the value of the global variable @code{x}, you must also |
2679
|
99 declare it to be global within the function body, like this |
|
100 |
|
101 @example |
|
102 @group |
|
103 function f () |
|
104 global x; |
|
105 x = 1; |
|
106 endfunction |
|
107 @end group |
|
108 @end example |
|
109 |
|
110 Passing a global variable in a function parameter list will |
|
111 make a local copy and not modify the global value. For example, given |
|
112 the function |
|
113 |
|
114 @example |
|
115 @group |
|
116 function f (x) |
|
117 x = 0 |
|
118 endfunction |
|
119 @end group |
|
120 @end example |
|
121 |
|
122 @noindent |
2689
|
123 and the definition of @code{x} as a global variable at the top level, |
2679
|
124 |
|
125 @example |
|
126 global x = 13 |
|
127 @end example |
|
128 |
|
129 @noindent |
|
130 the expression |
|
131 |
|
132 @example |
|
133 f (x) |
|
134 @end example |
|
135 |
|
136 @noindent |
2689
|
137 will display the value of @code{x} from inside the function as 0, |
|
138 but the value of @code{x} at the top level remains unchanged, because |
2679
|
139 the function works with a @emph{copy} of its argument. |
|
140 |
|
141 @defvr {Built-in Variable} warn_comma_in_global_decl |
|
142 If the value of @code{warn_comma_in_global_decl} is nonzero, a |
|
143 warning is issued for statements like |
|
144 |
|
145 @example |
|
146 global a = 1, b |
|
147 @end example |
|
148 |
|
149 @noindent |
2689
|
150 which makes the variables @code{a} and @code{b} global and assigns the |
|
151 value 1 to the variable @code{a}, because in this context, the comma is |
2679
|
152 not interpreted as a statement separator. |
|
153 |
|
154 The default value of @code{warn_comma_in_global_decl} is nonzero. |
|
155 @end defvr |
|
156 |
|
157 @deftypefn {Built-in Function} {} is_global (@var{name}) |
|
158 Return 1 if @var{name} is globally visible. Otherwise, return 0. For |
|
159 example, |
|
160 |
|
161 @example |
|
162 @group |
|
163 global x |
|
164 is_global ("x") |
|
165 @result{} 1 |
|
166 @end group |
|
167 @end example |
|
168 @end deftypefn |
|
169 |
|
170 @node Status of Variables, Summary of Built-in Variables, Global Variables, Variables |
|
171 @section Status of Variables |
|
172 |
|
173 @deffn {Command} clear options pattern @dots{} |
|
174 Delete the names matching the given patterns from the symbol table. The |
|
175 pattern may contain the following special characters: |
|
176 @table @code |
|
177 @item ? |
|
178 Match any single character. |
|
179 |
|
180 @item * |
|
181 Match zero or more characters. |
|
182 |
|
183 @item [ @var{list} ] |
|
184 Match the list of characters specified by @var{list}. If the first |
|
185 character is @code{!} or @code{^}, match all characters except those |
|
186 specified by @var{list}. For example, the pattern @samp{[a-zA-Z]} will |
|
187 match all lower and upper case alphabetic characters. |
|
188 @end table |
|
189 |
|
190 For example, the command |
|
191 |
|
192 @example |
|
193 clear foo b*r |
|
194 @end example |
|
195 |
|
196 @noindent |
|
197 clears the name @code{foo} and all names that begin with the letter |
|
198 @code{b} and end with the letter @code{r}. |
|
199 |
|
200 If @code{clear} is called without any arguments, all user-defined |
|
201 variables (local and global) are cleared from the symbol table. If |
|
202 @code{clear} is called with at least one argument, only the visible |
|
203 names matching the arguments are cleared. For example, suppose you have |
|
204 defined a function @code{foo}, and then hidden it by performing the |
2689
|
205 assignment @code{foo = 2}. Executing the command @kbd{clear foo} once |
2679
|
206 will clear the variable definition and restore the definition of |
2689
|
207 @code{foo} as a function. Executing @kbd{clear foo} a second time will |
2679
|
208 clear the function definition. |
|
209 |
|
210 This command may not be used within a function body. |
|
211 @end deffn |
|
212 |
|
213 @deffn {Command} who options pattern @dots{} |
|
214 @deffnx {Command} whos options pattern @dots{} |
|
215 List currently defined symbols matching the given patterns. The |
|
216 following are valid options. They may be shortened to one character but |
|
217 may not be combined. |
|
218 |
|
219 @table @code |
|
220 @item -all |
|
221 List all currently defined symbols. |
|
222 |
|
223 @item -builtins |
|
224 List built-in variables and functions. This includes all currently |
|
225 compiled function files, but does not include all function files that |
|
226 are in the @code{LOADPATH}. |
|
227 |
|
228 @item -functions |
|
229 List user-defined functions. |
|
230 |
|
231 @item -long |
|
232 Print a long listing including the type and dimensions of any symbols. |
|
233 The symbols in the first column of output indicate whether it is |
|
234 possible to redefine the symbol, and whether it is possible for it to be |
|
235 cleared. |
|
236 |
|
237 @item -variables |
|
238 List user-defined variables. |
|
239 @end table |
|
240 |
|
241 Valid patterns are the same as described for the @code{clear} command |
|
242 above. If no patterns are supplied, all symbols from the given category |
|
243 are listed. By default, only user defined functions and variables |
|
244 visible in the local scope are displayed. |
|
245 |
2689
|
246 The command @kbd{whos} is equivalent to @kbd{who -long}. |
2679
|
247 @end deffn |
|
248 |
|
249 @deftypefn {Built-in Function} {} exist (@var{name}) |
|
250 Return 1 if the name exists as a variable, and 2 if the name (after |
|
251 appending @samp{.m}) is a function file in the path. Otherwise, return |
|
252 0. |
|
253 @end deftypefn |
|
254 |
|
255 @deftypefn {Built-in Function} {} document (@var{symbol}, @var{text}) |
|
256 Set the documentation string for @var{symbol} to @var{text}. |
|
257 @end deftypefn |
|
258 |
|
259 @deffn {Command} type options name @dots{} |
|
260 Display the definition of each @var{name} that refers to a function. |
|
261 |
|
262 Normally also displays if each @var{name} is user-defined or builtin; |
|
263 the @code{-q} option suppresses this behaviour. |
|
264 |
|
265 Currently, Octave can only display functions that can be compiled |
|
266 cleanly, because it uses its internal representation of the function to |
|
267 recreate the program text. |
|
268 |
|
269 Comments are not displayed because Octave's parser currently discards |
|
270 them as it converts the text of a function file to its internal |
|
271 representation. This problem may be fixed in a future release. |
|
272 @end deffn |
|
273 |
|
274 @deffn {Command} which name @dots{} |
|
275 Display the type of each @var{name}. If @var{name} is defined from a |
|
276 function file, the full name of the file is also displayed. |
|
277 @end deffn |
|
278 |
|
279 @node Summary of Built-in Variables, Defaults from the Environment, Status of Variables, Variables |
|
280 @section Summary of Built-in Variables |
|
281 |
|
282 Here is a summary of all of Octave's built-in variables along with |
|
283 cross references to additional information and their default values. In |
2993
|
284 the following table @var{octave-home} stands for the root directory |
|
285 where all of Octave is installed (the default is @file{@value{OCTAVEHOME}}, |
|
286 @var{version} stands for the Octave version number (for example, |
|
287 @value{VERSION}) and @var{arch} stands for the type of system for which |
2701
|
288 Octave was compiled (for example, @code{@value{TARGETHOSTTYPE}}). |
2679
|
289 |
|
290 @vtable @code |
|
291 @item EDITOR |
|
292 @xref{Commands For History}. |
|
293 |
2993
|
294 Default value: @code{"emacs"}. |
2679
|
295 |
|
296 @item EXEC_PATH |
|
297 @xref{Controlling Subprocesses}. |
|
298 |
|
299 Default value: @code{":$PATH"}. |
|
300 |
|
301 @item INFO_FILE |
|
302 @xref{Getting Help}. |
|
303 |
2993
|
304 Default value: @code{"@var{octave-home}/info/octave.info"}. |
2679
|
305 |
|
306 @item INFO_PROGRAM |
|
307 @xref{Getting Help}. |
|
308 |
2993
|
309 Default value: @code{"@var{octave-home}/libexec/octave/@var{version}/exec/@var{arch}/info"}. |
2679
|
310 |
|
311 @item LOADPATH |
|
312 @xref{Function Files}. |
|
313 |
2993
|
314 Default value: @code{".:@var{octave-home}/lib/@var{version}"}. |
|
315 |
|
316 @item OCTAVE_HOME |
|
317 |
|
318 Default value: @code{"@value{OCTAVEHOME}"}. |
2679
|
319 |
|
320 @item PAGER |
|
321 @xref{Input and Output}. |
|
322 |
|
323 Default value: @code{"less", or "more"}. |
|
324 |
|
325 @item PS1 |
|
326 @xref{Customizing the Prompt}. |
|
327 |
|
328 Default value: @code{"\s:\#> "}. |
|
329 |
|
330 @item PS2 |
|
331 @xref{Customizing the Prompt}. |
|
332 |
|
333 Default value: @code{"> "}. |
|
334 |
|
335 @item PS4 |
|
336 @xref{Customizing the Prompt}. |
|
337 |
|
338 Default value: @code{"+ "}. |
|
339 |
|
340 @item automatic_replot |
|
341 @xref{Two-Dimensional Plotting}. |
|
342 |
|
343 Default value: 0. |
|
344 |
|
345 @item beep_on_error |
|
346 @xref{Error Handling}. |
|
347 |
|
348 Default value: 0. |
|
349 |
|
350 @item completion_append_char |
|
351 @xref{Commands For Completion}. |
|
352 |
|
353 Default value: @code{" "}. |
|
354 |
2993
|
355 @item default_eval_print_flag |
|
356 @xref{Evaluation}. |
|
357 |
|
358 Default value: 1. |
|
359 |
2679
|
360 @item default_return_value |
|
361 @xref{Multiple Return Values}. |
|
362 |
|
363 Default value: @code{[]}. |
|
364 |
2762
|
365 @item default_save_format |
|
366 @xref{Simple File I/O}. |
|
367 |
|
368 Default value: @code{"ascii"}. |
|
369 |
2679
|
370 @item do_fortran_indexing |
|
371 @xref{Index Expressions}. |
|
372 |
|
373 Default value: 0. |
|
374 |
|
375 @item define_all_return_values |
|
376 @xref{Multiple Return Values}. |
|
377 |
|
378 Default value: 0. |
|
379 |
|
380 @item empty_list_elements_ok |
|
381 @xref{Empty Matrices}. |
|
382 |
|
383 Default value: @code{"warn"}. |
|
384 |
|
385 @item gnuplot_binary |
|
386 @xref{Three-Dimensional Plotting}. |
|
387 |
|
388 Default value: @code{"gnuplot"}. |
|
389 |
|
390 @item history_file |
|
391 @xref{Commands For History}. |
|
392 |
|
393 Default value: @code{"~/.octave_hist"}. |
|
394 |
|
395 @item history_size |
|
396 @xref{Commands For History}. |
|
397 |
|
398 Default value: 1024. |
|
399 |
|
400 @item ignore_function_time_stamp |
|
401 @xref{Function Files}. |
|
402 |
|
403 Default value: @code{"system"}. |
|
404 |
|
405 @item implicit_str_to_num_ok |
|
406 @xref{String Conversions}. |
|
407 |
|
408 Default value: 0. |
|
409 |
|
410 @item ok_to_lose_imaginary_part |
|
411 @xref{Special Utility Matrices}. |
|
412 |
|
413 Default value: @code{"warn"}. |
|
414 |
|
415 @item output_max_field_width |
|
416 @xref{Matrices}. |
|
417 |
|
418 Default value: 10. |
|
419 |
|
420 @item output_precision |
|
421 @xref{Matrices}. |
|
422 |
|
423 Default value: 5. |
|
424 |
|
425 @item page_screen_output |
|
426 @xref{Input and Output}. |
|
427 |
|
428 Default value: 1. |
|
429 |
|
430 @item prefer_column_vectors |
|
431 @xref{Index Expressions}. |
|
432 |
|
433 Default value: 0. |
|
434 |
|
435 @item print_answer_id_name |
|
436 @xref{Terminal Output}. |
|
437 |
|
438 Default value: 1. |
|
439 |
|
440 @item print_empty_dimensions |
|
441 @xref{Empty Matrices}. |
|
442 |
|
443 Default value: 1. |
|
444 |
|
445 @item resize_on_range_error |
|
446 @xref{Index Expressions}. |
|
447 |
|
448 Default value: 1. |
|
449 |
|
450 @item return_last_computed_value |
|
451 @xref{Returning From a Function}. |
|
452 |
|
453 Default value: 0. |
|
454 |
|
455 @item save_precision |
|
456 @xref{Simple File I/O}. |
|
457 |
|
458 Default value: 17. |
|
459 |
|
460 @item saving_history |
|
461 @xref{Commands For History}. |
|
462 |
|
463 Default value: 1. |
|
464 |
|
465 @item silent_functions |
|
466 @xref{Defining Functions}. |
|
467 |
|
468 Default value: 0. |
|
469 |
|
470 @item split_long_rows |
|
471 @xref{Matrices}. |
|
472 |
|
473 Default value: 1. |
|
474 |
|
475 @item struct_levels_to_print |
|
476 @xref{Data Structures}. |
|
477 |
|
478 Default value: 2. |
|
479 |
|
480 @item suppress_verbose_help_message |
|
481 @xref{Getting Help}. |
|
482 |
|
483 Default value: 1. |
|
484 |
|
485 @item treat_neg_dim_as_zero |
|
486 @xref{Special Utility Matrices}. |
|
487 |
|
488 Default value: 0. |
|
489 |
|
490 @item warn_assign_as_truth_value |
|
491 @xref{The if Statement}. |
|
492 |
|
493 Default value: 1. |
|
494 |
|
495 @item warn_comma_in_global_decl |
|
496 @xref{Global Variables}. |
|
497 |
|
498 Default value: 1. |
|
499 |
|
500 @item warn_divide_by_zero |
|
501 @xref{Arithmetic Ops}. |
|
502 |
|
503 Default value: 1. |
|
504 |
|
505 @item warn_function_name_clash |
|
506 @xref{Function Files}. |
|
507 |
|
508 Default value: 1. |
|
509 |
|
510 @item whitespace_in_literal_matrix |
|
511 @xref{Matrices}. |
|
512 |
|
513 Default value: @code{""}. |
|
514 @end vtable |
|
515 |
|
516 |
|
517 @node Defaults from the Environment, , Summary of Built-in Variables, Variables |
|
518 @section Defaults from the Environment |
|
519 |
|
520 Octave uses the values of the following environment variables to set the |
|
521 default values for the corresponding built-in variables. In addition, |
|
522 the values from the environment may be overridden by command-line |
|
523 arguments. @xref{Command Line Options}. |
|
524 |
|
525 @vtable @code |
|
526 @item EDITOR |
|
527 @xref{Commands For History}. |
|
528 |
|
529 Built-in variable: @code{EDITOR}. |
|
530 |
|
531 @item OCTAVE_EXEC_PATH |
|
532 @xref{Controlling Subprocesses}. |
|
533 |
|
534 Built-in variable: @code{EXEC_PATH}. |
|
535 Command-line argument: @code{--exec-path}. |
|
536 |
|
537 @item OCTAVE_PATH |
|
538 @xref{Function Files}. |
|
539 |
|
540 Built-in variable: @code{LOADPATH}. |
|
541 Command-line argument: @code{--path}. |
|
542 |
|
543 @item OCTAVE_INFO_FILE |
|
544 @xref{Getting Help}. |
|
545 |
|
546 Built-in variable: @code{INFO_FILE}. |
|
547 Command-line argument: @code{--info-file}. |
|
548 |
|
549 @item OCTAVE_INFO_PROGRAM |
|
550 @xref{Getting Help}. |
|
551 |
|
552 Built-in variable: @code{INFO_PROGRAM}. |
|
553 Command-line argument: @code{--info-program}. |
|
554 |
|
555 @item OCTAVE_HISTSIZE |
|
556 @xref{Commands For History}. |
|
557 |
|
558 Built-in variable: @code{history_size}. |
|
559 |
|
560 @item OCTAVE_HISTFILE |
|
561 @xref{Commands For History}. |
|
562 |
|
563 Built-in variable: @code{history_file}. |
|
564 @end vtable |