3294
|
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 |
4167
|
5 @node Variables |
3294
|
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{ans} 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:: |
4686
|
58 * Persistent Variables:: |
3294
|
59 * Status of Variables:: |
|
60 * Summary of Built-in Variables:: |
|
61 * Defaults from the Environment:: |
|
62 @end menu |
|
63 |
4167
|
64 @node Global Variables |
3294
|
65 @section Global Variables |
|
66 @cindex global variables |
|
67 @cindex @code{global} statement |
|
68 @cindex variables, global |
|
69 |
|
70 A variable that has been declared @dfn{global} may be accessed from |
|
71 within a function body without having to pass it as a formal parameter. |
|
72 |
|
73 A variable may be declared global using a @code{global} declaration |
|
74 statement. The following statements are all global declarations. |
|
75 |
|
76 @example |
|
77 @group |
|
78 global a |
4504
|
79 global a b |
|
80 global c = 2 |
|
81 global d = 3 e f = 5 |
3294
|
82 @end group |
|
83 @end example |
|
84 |
4504
|
85 A global variable may only be initialized once in a @code{global} |
|
86 statement. For example, after executing the following code |
|
87 |
|
88 @example |
|
89 @group |
|
90 global gvar = 1 |
|
91 global gvar = 2 |
|
92 @end group |
|
93 @end example |
|
94 |
|
95 @noindent |
|
96 the value of the global variable @code{gvar} is 1, not 2. |
|
97 |
3294
|
98 It is necessary declare a variable as global within a function body in |
|
99 order to access it. For example, |
|
100 |
|
101 @example |
|
102 @group |
|
103 global x |
|
104 function f () |
|
105 x = 1; |
|
106 endfunction |
|
107 f () |
|
108 @end group |
|
109 @end example |
|
110 |
|
111 @noindent |
|
112 does @emph{not} set the value of the global variable @code{x} to 1. In |
|
113 order to change the value of the global variable @code{x}, you must also |
|
114 declare it to be global within the function body, like this |
|
115 |
|
116 @example |
|
117 @group |
|
118 function f () |
|
119 global x; |
|
120 x = 1; |
|
121 endfunction |
|
122 @end group |
|
123 @end example |
|
124 |
|
125 Passing a global variable in a function parameter list will |
|
126 make a local copy and not modify the global value. For example, given |
|
127 the function |
|
128 |
|
129 @example |
|
130 @group |
|
131 function f (x) |
|
132 x = 0 |
|
133 endfunction |
|
134 @end group |
|
135 @end example |
|
136 |
|
137 @noindent |
|
138 and the definition of @code{x} as a global variable at the top level, |
|
139 |
|
140 @example |
|
141 global x = 13 |
|
142 @end example |
|
143 |
|
144 @noindent |
|
145 the expression |
|
146 |
|
147 @example |
|
148 f (x) |
|
149 @end example |
|
150 |
|
151 @noindent |
|
152 will display the value of @code{x} from inside the function as 0, |
|
153 but the value of @code{x} at the top level remains unchanged, because |
|
154 the function works with a @emph{copy} of its argument. |
|
155 |
4029
|
156 @DOCSTRING(isglobal) |
3294
|
157 |
4686
|
158 @node Persistent Variables |
|
159 @section Persistent Variables |
|
160 @cindex persistent variables |
|
161 @cindex @code{persistent} statement |
|
162 @cindex variables, persistent |
|
163 |
|
164 A variable that has been declared @dfn{persistent} within a function |
|
165 will retain its contents in memory between subsequent calls to the |
|
166 same function. The difference between persistent variables and global |
|
167 variables is that persistent variables are local in scope to a |
|
168 particular function and are not visible elsewhere. |
|
169 |
|
170 A variable may be declared persistent using a @code{persistent} |
|
171 declaration statement. The following statements are all persistent |
|
172 declarations. |
|
173 |
|
174 @example |
|
175 @group |
|
176 persistent a |
|
177 persistent a b |
|
178 persistent c = 2 |
|
179 persistent d = 3 e f = 5 |
|
180 @end group |
|
181 @end example |
|
182 |
|
183 The behavior of persistent variables is equivalent to the behavior of |
|
184 static variables in C. The command @code{static} in octave is also |
|
185 recognized and is equivalent to @code{persistent}. Unlike global |
|
186 variables, every initialization statement will re-initialize the |
|
187 variable. For example, after executing the following code |
|
188 |
|
189 @example |
|
190 @group |
|
191 persistent pvar = 1 |
|
192 persistent pvar = 2 |
|
193 @end group |
|
194 @end example |
|
195 |
|
196 @noindent |
|
197 the value of the persistent variable @code{pvar} is 2. |
|
198 |
4167
|
199 @node Status of Variables |
3294
|
200 @section Status of Variables |
|
201 |
3361
|
202 @DOCSTRING(clear) |
3294
|
203 |
3361
|
204 @DOCSTRING(who) |
3294
|
205 |
4913
|
206 @DOCSTRING(whos) |
|
207 |
|
208 @DOCSTRING(whos_line_format) |
|
209 |
3361
|
210 @DOCSTRING(exist) |
3294
|
211 |
3361
|
212 @DOCSTRING(document) |
3294
|
213 |
3361
|
214 @DOCSTRING(type) |
3294
|
215 |
3361
|
216 @DOCSTRING(which) |
3294
|
217 |
4167
|
218 @node Summary of Built-in Variables |
3294
|
219 @section Summary of Built-in Variables |
|
220 |
|
221 Here is a summary of all of Octave's built-in variables along with |
|
222 cross references to additional information and their default values. In |
|
223 the following table @var{octave-home} stands for the root directory |
|
224 where all of Octave is installed (the default is @file{@value{OCTAVEHOME}}, |
|
225 @var{version} stands for the Octave version number (for example, |
|
226 @value{VERSION}) and @var{arch} stands for the type of system for which |
5942
|
227 Octave was compiled (for example, @code{x86_64-unknown-linux-gnu}). |
3294
|
228 |
|
229 @vtable @code |
|
230 @item DEFAULT_LOADPATH |
|
231 @xref{Function Files}. |
|
232 |
|
233 Default value: @code{".:@var{octave-home}/lib/@var{version}"}. |
|
234 |
|
235 @item EDITOR |
|
236 @xref{Commands For History}. |
|
237 |
|
238 Default value: @code{"emacs"}. |
|
239 |
|
240 @item EXEC_PATH |
|
241 @xref{Controlling Subprocesses}. |
|
242 |
|
243 Default value: @code{":$PATH"}. |
|
244 |
|
245 @item INFO_FILE |
|
246 @xref{Getting Help}. |
|
247 |
|
248 Default value: @code{"@var{octave-home}/info/octave.info"}. |
|
249 |
|
250 @item INFO_PROGRAM |
|
251 @xref{Getting Help}. |
|
252 |
|
253 Default value: @code{"@var{octave-home}/libexec/octave/@var{version}/exec/@var{arch}/info"}. |
|
254 |
|
255 @item LOADPATH |
|
256 @xref{Function Files}. |
|
257 |
|
258 Default value: @code{":"}, which tells Octave to use the directories |
|
259 specified by the built-in variable @code{DEFAULT_LOADPATH}. |
|
260 |
|
261 @item OCTAVE_HOME |
|
262 |
|
263 Default value: @code{"@value{OCTAVEHOME}"}. |
|
264 |
|
265 @item PAGER |
|
266 @xref{Input and Output}. |
|
267 |
|
268 Default value: @code{"less", or "more"}. |
|
269 |
|
270 @item PS1 |
|
271 @xref{Customizing the Prompt}. |
|
272 |
|
273 Default value: @code{"\s:\#> "}. |
|
274 |
|
275 @item PS2 |
|
276 @xref{Customizing the Prompt}. |
|
277 |
|
278 Default value: @code{"> "}. |
|
279 |
|
280 @item PS4 |
|
281 @xref{Customizing the Prompt}. |
|
282 |
|
283 Default value: @code{"+ "}. |
|
284 |
|
285 @item automatic_replot |
5763
|
286 @xref{Low-Level plotting commands}. |
3294
|
287 |
|
288 Default value: 0. |
|
289 |
|
290 @item beep_on_error |
|
291 @xref{Error Handling}. |
|
292 |
|
293 Default value: 0. |
|
294 |
|
295 @item completion_append_char |
|
296 @xref{Commands For Completion}. |
|
297 |
|
298 Default value: @code{" "}. |
|
299 |
5287
|
300 @item default_save_options |
3294
|
301 @xref{Simple File I/O}. |
|
302 |
|
303 Default value: @code{"ascii"}. |
|
304 |
|
305 @item crash_dumps_octave_core |
|
306 @xref{Simple File I/O}. |
|
307 |
|
308 Default value: 1. |
|
309 |
|
310 @item fixed_point_format |
|
311 @xref{Matrices}. |
|
312 |
|
313 Default value: 0. |
|
314 |
|
315 @item gnuplot_binary |
|
316 @xref{Three-Dimensional Plotting}. |
|
317 |
|
318 Default value: @code{"gnuplot"}. |
|
319 |
|
320 @item history_file |
|
321 @xref{Commands For History}. |
|
322 |
|
323 Default value: @code{"~/.octave_hist"}. |
|
324 |
|
325 @item history_size |
|
326 @xref{Commands For History}. |
|
327 |
|
328 Default value: 1024. |
|
329 |
|
330 @item ignore_function_time_stamp |
|
331 @xref{Function Files}. |
|
332 |
|
333 Default value: @code{"system"}. |
|
334 |
|
335 @item max_recursion_depth |
|
336 @xref{Recursion}. |
|
337 |
|
338 Default value: 256. |
|
339 |
|
340 @item output_max_field_width |
|
341 @xref{Matrices}. |
|
342 |
|
343 Default value: 10. |
|
344 |
|
345 @item output_precision |
|
346 @xref{Matrices}. |
|
347 |
|
348 Default value: 5. |
|
349 |
|
350 @item page_screen_output |
|
351 @xref{Input and Output}. |
|
352 |
|
353 Default value: 1. |
|
354 |
|
355 @item print_answer_id_name |
|
356 @xref{Terminal Output}. |
|
357 |
|
358 Default value: 1. |
|
359 |
|
360 @item print_empty_dimensions |
|
361 @xref{Empty Matrices}. |
|
362 |
|
363 Default value: 1. |
|
364 |
|
365 @item return_last_computed_value |
|
366 @xref{Returning From a Function}. |
|
367 |
|
368 Default value: 0. |
|
369 |
|
370 @item save_precision |
|
371 @xref{Simple File I/O}. |
|
372 |
|
373 Default value: 17. |
|
374 |
|
375 @item saving_history |
|
376 @xref{Commands For History}. |
|
377 |
|
378 Default value: 1. |
|
379 |
4449
|
380 @item sighup_dumps_octave_core |
|
381 @xref{Simple File I/O}. |
|
382 |
|
383 Default value: 1. |
|
384 |
|
385 @item sigterm_dumps_octave_core |
|
386 @xref{Simple File I/O}. |
|
387 |
|
388 Default value: 1. |
|
389 |
3294
|
390 @item silent_functions |
|
391 @xref{Defining Functions}. |
|
392 |
|
393 Default value: 0. |
|
394 |
|
395 @item split_long_rows |
|
396 @xref{Matrices}. |
|
397 |
|
398 Default value: 1. |
|
399 |
|
400 @item struct_levels_to_print |
|
401 @xref{Data Structures}. |
|
402 |
|
403 Default value: 2. |
|
404 |
|
405 @item suppress_verbose_help_message |
|
406 @xref{Getting Help}. |
|
407 |
|
408 Default value: 1. |
|
409 |
|
410 @item warn_assign_as_truth_value |
|
411 @xref{The if Statement}. |
|
412 |
|
413 Default value: 1. |
|
414 |
|
415 @item warn_comma_in_global_decl |
|
416 @xref{Global Variables}. |
|
417 |
|
418 Default value: 1. |
|
419 |
|
420 @item warn_divide_by_zero |
|
421 @xref{Arithmetic Ops}. |
|
422 |
|
423 Default value: 1. |
|
424 |
4460
|
425 @item warn_empty_list_elements |
|
426 @xref{Empty Matrices}. |
|
427 |
|
428 Default value: 0. |
|
429 |
4455
|
430 @item warn_fortran_indexing |
|
431 @xref{Index Expressions}. |
|
432 |
|
433 Default value: 0. |
|
434 |
4452
|
435 @item warn_function_name_clash |
|
436 @xref{Function Files}. |
|
437 |
|
438 Default value: 1. |
|
439 |
4451
|
440 @item warn_imag_to_real |
|
441 @xref{Special Utility Matrices}. |
|
442 |
|
443 Default value: 0. |
|
444 |
4452
|
445 @item warn_missing_semicolon |
|
446 @xref{Defining Functions}. |
|
447 |
|
448 Default value: 0. |
|
449 |
4457
|
450 @item warn_neg_dim_as_zero |
|
451 @xref{Special Utility Matrices}. |
|
452 |
|
453 Default value: 0. |
|
454 |
4452
|
455 @item warn_num_to_str |
|
456 @xref{String Conversions}. |
3294
|
457 |
|
458 Default value: 1. |
|
459 |
|
460 @item warn_reload_forces_clear |
|
461 @xref{Dynamically Linked Functions}. |
|
462 |
|
463 Default value: 1. |
|
464 |
4461
|
465 @item warn_resize_on_range_error |
|
466 @xref{Index Expressions}. |
|
467 |
|
468 Default value: 0. |
|
469 |
4476
|
470 @item warn_separator_insert |
|
471 @xref{Matrices}. |
|
472 |
|
473 Default value: 0. |
|
474 |
|
475 @item warn_single_quote_string |
4452
|
476 @xref{String Conversions}. |
|
477 |
|
478 Default value: 0. |
|
479 |
4466
|
480 @item warn_str_to_num |
|
481 @xref{String Conversions}. |
|
482 |
|
483 Default value: 0. |
|
484 |
|
485 @item warn_undefined_return_values |
|
486 @xref{Multiple Return Values}. |
|
487 |
|
488 Default value: 0. |
|
489 |
3294
|
490 @item warn_variable_switch_label |
|
491 @xref{The switch Statement}. |
|
492 |
|
493 Default value: 0. |
|
494 @end vtable |
|
495 |
|
496 |
4167
|
497 @node Defaults from the Environment |
3294
|
498 @section Defaults from the Environment |
|
499 |
|
500 Octave uses the values of the following environment variables to set the |
|
501 default values for the corresponding built-in variables. In addition, |
|
502 the values from the environment may be overridden by command-line |
|
503 arguments. @xref{Command Line Options}. |
|
504 |
|
505 @vtable @code |
|
506 @item EDITOR |
|
507 @xref{Commands For History}. |
|
508 |
|
509 Built-in variable: @code{EDITOR}. |
|
510 |
|
511 @item OCTAVE_EXEC_PATH |
|
512 @xref{Controlling Subprocesses}. |
|
513 |
|
514 Built-in variable: @code{EXEC_PATH}. |
|
515 Command-line argument: @code{--exec-path}. |
|
516 |
|
517 @item OCTAVE_PATH |
|
518 @xref{Function Files}. |
|
519 |
|
520 Built-in variable: @code{LOADPATH}. |
|
521 Command-line argument: @code{--path}. |
|
522 |
|
523 @item OCTAVE_INFO_FILE |
|
524 @xref{Getting Help}. |
|
525 |
|
526 Built-in variable: @code{INFO_FILE}. |
|
527 Command-line argument: @code{--info-file}. |
|
528 |
|
529 @item OCTAVE_INFO_PROGRAM |
|
530 @xref{Getting Help}. |
|
531 |
|
532 Built-in variable: @code{INFO_PROGRAM}. |
|
533 Command-line argument: @code{--info-program}. |
|
534 |
|
535 @item OCTAVE_HISTSIZE |
|
536 @xref{Commands For History}. |
|
537 |
|
538 Built-in variable: @code{history_size}. |
|
539 |
|
540 @item OCTAVE_HISTFILE |
|
541 @xref{Commands For History}. |
|
542 |
|
543 Built-in variable: @code{history_file}. |
|
544 @end vtable |