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}) |
3069
|
250 Return 1 if the name exists as a variable, 2 if the name (after |
|
251 appending @samp{.m}) is a function file in the path, 3 if the name is a |
|
252 @samp{.oct} file in the path, or 5 if the name is a built-in function. |
|
253 Otherwise, return 0. |
2679
|
254 @end deftypefn |
|
255 |
|
256 @deftypefn {Built-in Function} {} document (@var{symbol}, @var{text}) |
|
257 Set the documentation string for @var{symbol} to @var{text}. |
|
258 @end deftypefn |
|
259 |
|
260 @deffn {Command} type options name @dots{} |
|
261 Display the definition of each @var{name} that refers to a function. |
|
262 |
|
263 Normally also displays if each @var{name} is user-defined or builtin; |
|
264 the @code{-q} option suppresses this behaviour. |
|
265 |
|
266 Currently, Octave can only display functions that can be compiled |
|
267 cleanly, because it uses its internal representation of the function to |
|
268 recreate the program text. |
|
269 |
|
270 Comments are not displayed because Octave's parser currently discards |
|
271 them as it converts the text of a function file to its internal |
|
272 representation. This problem may be fixed in a future release. |
|
273 @end deffn |
|
274 |
|
275 @deffn {Command} which name @dots{} |
|
276 Display the type of each @var{name}. If @var{name} is defined from a |
|
277 function file, the full name of the file is also displayed. |
|
278 @end deffn |
|
279 |
|
280 @node Summary of Built-in Variables, Defaults from the Environment, Status of Variables, Variables |
|
281 @section Summary of Built-in Variables |
|
282 |
|
283 Here is a summary of all of Octave's built-in variables along with |
|
284 cross references to additional information and their default values. In |
2993
|
285 the following table @var{octave-home} stands for the root directory |
|
286 where all of Octave is installed (the default is @file{@value{OCTAVEHOME}}, |
|
287 @var{version} stands for the Octave version number (for example, |
|
288 @value{VERSION}) and @var{arch} stands for the type of system for which |
2701
|
289 Octave was compiled (for example, @code{@value{TARGETHOSTTYPE}}). |
2679
|
290 |
|
291 @vtable @code |
|
292 @item EDITOR |
|
293 @xref{Commands For History}. |
|
294 |
2993
|
295 Default value: @code{"emacs"}. |
2679
|
296 |
|
297 @item EXEC_PATH |
|
298 @xref{Controlling Subprocesses}. |
|
299 |
|
300 Default value: @code{":$PATH"}. |
|
301 |
|
302 @item INFO_FILE |
|
303 @xref{Getting Help}. |
|
304 |
2993
|
305 Default value: @code{"@var{octave-home}/info/octave.info"}. |
2679
|
306 |
|
307 @item INFO_PROGRAM |
|
308 @xref{Getting Help}. |
|
309 |
2993
|
310 Default value: @code{"@var{octave-home}/libexec/octave/@var{version}/exec/@var{arch}/info"}. |
2679
|
311 |
|
312 @item LOADPATH |
|
313 @xref{Function Files}. |
|
314 |
2993
|
315 Default value: @code{".:@var{octave-home}/lib/@var{version}"}. |
|
316 |
|
317 @item OCTAVE_HOME |
|
318 |
|
319 Default value: @code{"@value{OCTAVEHOME}"}. |
2679
|
320 |
|
321 @item PAGER |
|
322 @xref{Input and Output}. |
|
323 |
|
324 Default value: @code{"less", or "more"}. |
|
325 |
|
326 @item PS1 |
|
327 @xref{Customizing the Prompt}. |
|
328 |
|
329 Default value: @code{"\s:\#> "}. |
|
330 |
|
331 @item PS2 |
|
332 @xref{Customizing the Prompt}. |
|
333 |
|
334 Default value: @code{"> "}. |
|
335 |
|
336 @item PS4 |
|
337 @xref{Customizing the Prompt}. |
|
338 |
|
339 Default value: @code{"+ "}. |
|
340 |
3131
|
341 @item auto_unload_dot_oct_files |
|
342 @xref{Dynamically Linked Functions}. |
|
343 |
|
344 Default value: 0. |
|
345 |
2679
|
346 @item automatic_replot |
|
347 @xref{Two-Dimensional Plotting}. |
|
348 |
|
349 Default value: 0. |
|
350 |
|
351 @item beep_on_error |
|
352 @xref{Error Handling}. |
|
353 |
|
354 Default value: 0. |
|
355 |
|
356 @item completion_append_char |
|
357 @xref{Commands For Completion}. |
|
358 |
|
359 Default value: @code{" "}. |
|
360 |
2993
|
361 @item default_eval_print_flag |
|
362 @xref{Evaluation}. |
|
363 |
|
364 Default value: 1. |
|
365 |
2679
|
366 @item default_return_value |
|
367 @xref{Multiple Return Values}. |
|
368 |
|
369 Default value: @code{[]}. |
|
370 |
2762
|
371 @item default_save_format |
|
372 @xref{Simple File I/O}. |
|
373 |
|
374 Default value: @code{"ascii"}. |
|
375 |
2679
|
376 @item do_fortran_indexing |
|
377 @xref{Index Expressions}. |
|
378 |
|
379 Default value: 0. |
|
380 |
|
381 @item define_all_return_values |
|
382 @xref{Multiple Return Values}. |
|
383 |
|
384 Default value: 0. |
|
385 |
|
386 @item empty_list_elements_ok |
|
387 @xref{Empty Matrices}. |
|
388 |
|
389 Default value: @code{"warn"}. |
|
390 |
3131
|
391 @item fixed_point_format |
|
392 @xref{Matrices}. |
|
393 |
|
394 Default value: 0. |
|
395 |
2679
|
396 @item gnuplot_binary |
|
397 @xref{Three-Dimensional Plotting}. |
|
398 |
|
399 Default value: @code{"gnuplot"}. |
|
400 |
|
401 @item history_file |
|
402 @xref{Commands For History}. |
|
403 |
|
404 Default value: @code{"~/.octave_hist"}. |
|
405 |
|
406 @item history_size |
|
407 @xref{Commands For History}. |
|
408 |
|
409 Default value: 1024. |
|
410 |
|
411 @item ignore_function_time_stamp |
|
412 @xref{Function Files}. |
|
413 |
|
414 Default value: @code{"system"}. |
|
415 |
3131
|
416 @item implicit_num_to_str_ok |
|
417 @xref{String Conversions}. |
|
418 |
|
419 Default value: 0. |
|
420 |
2679
|
421 @item implicit_str_to_num_ok |
|
422 @xref{String Conversions}. |
|
423 |
|
424 Default value: 0. |
|
425 |
3131
|
426 @item max_recursion_depth |
|
427 @xref{Recursion}. |
|
428 |
|
429 Default value: 256. |
|
430 |
2679
|
431 @item ok_to_lose_imaginary_part |
|
432 @xref{Special Utility Matrices}. |
|
433 |
|
434 Default value: @code{"warn"}. |
|
435 |
|
436 @item output_max_field_width |
|
437 @xref{Matrices}. |
|
438 |
|
439 Default value: 10. |
|
440 |
|
441 @item output_precision |
|
442 @xref{Matrices}. |
|
443 |
|
444 Default value: 5. |
|
445 |
|
446 @item page_screen_output |
|
447 @xref{Input and Output}. |
|
448 |
|
449 Default value: 1. |
|
450 |
|
451 @item prefer_column_vectors |
|
452 @xref{Index Expressions}. |
|
453 |
|
454 Default value: 0. |
|
455 |
|
456 @item print_answer_id_name |
|
457 @xref{Terminal Output}. |
|
458 |
|
459 Default value: 1. |
|
460 |
|
461 @item print_empty_dimensions |
|
462 @xref{Empty Matrices}. |
|
463 |
|
464 Default value: 1. |
|
465 |
|
466 @item resize_on_range_error |
|
467 @xref{Index Expressions}. |
|
468 |
|
469 Default value: 1. |
|
470 |
|
471 @item return_last_computed_value |
|
472 @xref{Returning From a Function}. |
|
473 |
|
474 Default value: 0. |
|
475 |
|
476 @item save_precision |
|
477 @xref{Simple File I/O}. |
|
478 |
|
479 Default value: 17. |
|
480 |
|
481 @item saving_history |
|
482 @xref{Commands For History}. |
|
483 |
|
484 Default value: 1. |
|
485 |
|
486 @item silent_functions |
|
487 @xref{Defining Functions}. |
|
488 |
|
489 Default value: 0. |
|
490 |
|
491 @item split_long_rows |
|
492 @xref{Matrices}. |
|
493 |
|
494 Default value: 1. |
|
495 |
|
496 @item struct_levels_to_print |
|
497 @xref{Data Structures}. |
|
498 |
|
499 Default value: 2. |
|
500 |
|
501 @item suppress_verbose_help_message |
|
502 @xref{Getting Help}. |
|
503 |
|
504 Default value: 1. |
|
505 |
|
506 @item treat_neg_dim_as_zero |
|
507 @xref{Special Utility Matrices}. |
|
508 |
|
509 Default value: 0. |
|
510 |
|
511 @item warn_assign_as_truth_value |
|
512 @xref{The if Statement}. |
|
513 |
|
514 Default value: 1. |
|
515 |
|
516 @item warn_comma_in_global_decl |
|
517 @xref{Global Variables}. |
|
518 |
|
519 Default value: 1. |
|
520 |
|
521 @item warn_divide_by_zero |
|
522 @xref{Arithmetic Ops}. |
|
523 |
|
524 Default value: 1. |
|
525 |
|
526 @item warn_function_name_clash |
|
527 @xref{Function Files}. |
|
528 |
|
529 Default value: 1. |
|
530 |
3131
|
531 @item warn_reload_forces_clear |
|
532 @xref{Dynamically Linked Functions}. |
|
533 |
|
534 Default value: 1. |
|
535 |
|
536 @item warn_variable_switch_label |
|
537 @xref{The switch Statement}. |
|
538 |
|
539 Default value: 0. |
|
540 |
2679
|
541 @item whitespace_in_literal_matrix |
|
542 @xref{Matrices}. |
|
543 |
|
544 Default value: @code{""}. |
|
545 @end vtable |
|
546 |
|
547 |
|
548 @node Defaults from the Environment, , Summary of Built-in Variables, Variables |
|
549 @section Defaults from the Environment |
|
550 |
|
551 Octave uses the values of the following environment variables to set the |
|
552 default values for the corresponding built-in variables. In addition, |
|
553 the values from the environment may be overridden by command-line |
|
554 arguments. @xref{Command Line Options}. |
|
555 |
|
556 @vtable @code |
|
557 @item EDITOR |
|
558 @xref{Commands For History}. |
|
559 |
|
560 Built-in variable: @code{EDITOR}. |
|
561 |
|
562 @item OCTAVE_EXEC_PATH |
|
563 @xref{Controlling Subprocesses}. |
|
564 |
|
565 Built-in variable: @code{EXEC_PATH}. |
|
566 Command-line argument: @code{--exec-path}. |
|
567 |
|
568 @item OCTAVE_PATH |
|
569 @xref{Function Files}. |
|
570 |
|
571 Built-in variable: @code{LOADPATH}. |
|
572 Command-line argument: @code{--path}. |
|
573 |
|
574 @item OCTAVE_INFO_FILE |
|
575 @xref{Getting Help}. |
|
576 |
|
577 Built-in variable: @code{INFO_FILE}. |
|
578 Command-line argument: @code{--info-file}. |
|
579 |
|
580 @item OCTAVE_INFO_PROGRAM |
|
581 @xref{Getting Help}. |
|
582 |
|
583 Built-in variable: @code{INFO_PROGRAM}. |
|
584 Command-line argument: @code{--info-program}. |
|
585 |
|
586 @item OCTAVE_HISTSIZE |
|
587 @xref{Commands For History}. |
|
588 |
|
589 Built-in variable: @code{history_size}. |
|
590 |
|
591 @item OCTAVE_HISTFILE |
|
592 @xref{Commands For History}. |
|
593 |
|
594 Built-in variable: @code{history_file}. |
|
595 @end vtable |