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:: |
|
58 * Status of Variables:: |
|
59 * Summary of Built-in Variables:: |
|
60 * Defaults from the Environment:: |
|
61 @end menu |
|
62 |
4167
|
63 @node Global Variables |
3294
|
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 () |
|
90 x = 1; |
|
91 endfunction |
|
92 f () |
|
93 @end group |
|
94 @end example |
|
95 |
|
96 @noindent |
|
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 |
|
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 |
|
123 and the definition of @code{x} as a global variable at the top level, |
|
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 |
|
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 |
|
139 the function works with a @emph{copy} of its argument. |
|
140 |
3428
|
141 @DOCSTRING(initialize_global_variables) |
|
142 |
3361
|
143 @DOCSTRING(default_global_variable_value) |
3294
|
144 |
4029
|
145 @DOCSTRING(isglobal) |
3294
|
146 |
4167
|
147 @node Status of Variables |
3294
|
148 @section Status of Variables |
|
149 |
3361
|
150 @DOCSTRING(clear) |
3294
|
151 |
3361
|
152 @DOCSTRING(who) |
3294
|
153 |
3361
|
154 @DOCSTRING(exist) |
3294
|
155 |
3361
|
156 @DOCSTRING(document) |
3294
|
157 |
3361
|
158 @DOCSTRING(type) |
3294
|
159 |
3361
|
160 @DOCSTRING(which) |
3294
|
161 |
4167
|
162 @node Summary of Built-in Variables |
3294
|
163 @section Summary of Built-in Variables |
|
164 |
|
165 Here is a summary of all of Octave's built-in variables along with |
|
166 cross references to additional information and their default values. In |
|
167 the following table @var{octave-home} stands for the root directory |
|
168 where all of Octave is installed (the default is @file{@value{OCTAVEHOME}}, |
|
169 @var{version} stands for the Octave version number (for example, |
|
170 @value{VERSION}) and @var{arch} stands for the type of system for which |
|
171 Octave was compiled (for example, @code{@value{TARGETHOSTTYPE}}). |
|
172 |
|
173 @vtable @code |
|
174 @item DEFAULT_LOADPATH |
|
175 @xref{Function Files}. |
|
176 |
|
177 Default value: @code{".:@var{octave-home}/lib/@var{version}"}. |
|
178 |
|
179 @item EDITOR |
|
180 @xref{Commands For History}. |
|
181 |
|
182 Default value: @code{"emacs"}. |
|
183 |
|
184 @item EXEC_PATH |
|
185 @xref{Controlling Subprocesses}. |
|
186 |
|
187 Default value: @code{":$PATH"}. |
|
188 |
|
189 @item INFO_FILE |
|
190 @xref{Getting Help}. |
|
191 |
|
192 Default value: @code{"@var{octave-home}/info/octave.info"}. |
|
193 |
|
194 @item INFO_PROGRAM |
|
195 @xref{Getting Help}. |
|
196 |
|
197 Default value: @code{"@var{octave-home}/libexec/octave/@var{version}/exec/@var{arch}/info"}. |
|
198 |
|
199 @item LOADPATH |
|
200 @xref{Function Files}. |
|
201 |
|
202 Default value: @code{":"}, which tells Octave to use the directories |
|
203 specified by the built-in variable @code{DEFAULT_LOADPATH}. |
|
204 |
|
205 @item OCTAVE_HOME |
|
206 |
|
207 Default value: @code{"@value{OCTAVEHOME}"}. |
|
208 |
|
209 @item PAGER |
|
210 @xref{Input and Output}. |
|
211 |
|
212 Default value: @code{"less", or "more"}. |
|
213 |
|
214 @item PS1 |
|
215 @xref{Customizing the Prompt}. |
|
216 |
|
217 Default value: @code{"\s:\#> "}. |
|
218 |
|
219 @item PS2 |
|
220 @xref{Customizing the Prompt}. |
|
221 |
|
222 Default value: @code{"> "}. |
|
223 |
|
224 @item PS4 |
|
225 @xref{Customizing the Prompt}. |
|
226 |
|
227 Default value: @code{"+ "}. |
|
228 |
|
229 @item automatic_replot |
|
230 @xref{Two-Dimensional Plotting}. |
|
231 |
|
232 Default value: 0. |
|
233 |
|
234 @item beep_on_error |
|
235 @xref{Error Handling}. |
|
236 |
|
237 Default value: 0. |
|
238 |
|
239 @item completion_append_char |
|
240 @xref{Commands For Completion}. |
|
241 |
|
242 Default value: @code{" "}. |
|
243 |
|
244 @item default_eval_print_flag |
|
245 @xref{Evaluation}. |
|
246 |
|
247 Default value: 1. |
|
248 |
|
249 @item default_return_value |
|
250 @xref{Multiple Return Values}. |
|
251 |
|
252 Default value: @code{[]}. |
|
253 |
|
254 @item default_save_format |
|
255 @xref{Simple File I/O}. |
|
256 |
|
257 Default value: @code{"ascii"}. |
|
258 |
|
259 @item crash_dumps_octave_core |
|
260 @xref{Simple File I/O}. |
|
261 |
|
262 Default value: 1. |
|
263 |
|
264 @item define_all_return_values |
|
265 @xref{Multiple Return Values}. |
|
266 |
|
267 Default value: 0. |
|
268 |
|
269 @item fixed_point_format |
|
270 @xref{Matrices}. |
|
271 |
|
272 Default value: 0. |
|
273 |
|
274 @item gnuplot_binary |
|
275 @xref{Three-Dimensional Plotting}. |
|
276 |
|
277 Default value: @code{"gnuplot"}. |
|
278 |
|
279 @item history_file |
|
280 @xref{Commands For History}. |
|
281 |
|
282 Default value: @code{"~/.octave_hist"}. |
|
283 |
|
284 @item history_size |
|
285 @xref{Commands For History}. |
|
286 |
|
287 Default value: 1024. |
|
288 |
|
289 @item ignore_function_time_stamp |
|
290 @xref{Function Files}. |
|
291 |
|
292 Default value: @code{"system"}. |
|
293 |
|
294 @item max_recursion_depth |
|
295 @xref{Recursion}. |
|
296 |
|
297 Default value: 256. |
|
298 |
|
299 @item output_max_field_width |
|
300 @xref{Matrices}. |
|
301 |
|
302 Default value: 10. |
|
303 |
|
304 @item output_precision |
|
305 @xref{Matrices}. |
|
306 |
|
307 Default value: 5. |
|
308 |
|
309 @item page_screen_output |
|
310 @xref{Input and Output}. |
|
311 |
|
312 Default value: 1. |
|
313 |
|
314 @item print_answer_id_name |
|
315 @xref{Terminal Output}. |
|
316 |
|
317 Default value: 1. |
|
318 |
|
319 @item print_empty_dimensions |
|
320 @xref{Empty Matrices}. |
|
321 |
|
322 Default value: 1. |
|
323 |
|
324 @item return_last_computed_value |
|
325 @xref{Returning From a Function}. |
|
326 |
|
327 Default value: 0. |
|
328 |
|
329 @item save_precision |
|
330 @xref{Simple File I/O}. |
|
331 |
|
332 Default value: 17. |
|
333 |
|
334 @item saving_history |
|
335 @xref{Commands For History}. |
|
336 |
|
337 Default value: 1. |
|
338 |
4449
|
339 @item sighup_dumps_octave_core |
|
340 @xref{Simple File I/O}. |
|
341 |
|
342 Default value: 1. |
|
343 |
|
344 @item sigterm_dumps_octave_core |
|
345 @xref{Simple File I/O}. |
|
346 |
|
347 Default value: 1. |
|
348 |
3294
|
349 @item silent_functions |
|
350 @xref{Defining Functions}. |
|
351 |
|
352 Default value: 0. |
|
353 |
|
354 @item split_long_rows |
|
355 @xref{Matrices}. |
|
356 |
|
357 Default value: 1. |
|
358 |
|
359 @item struct_levels_to_print |
|
360 @xref{Data Structures}. |
|
361 |
|
362 Default value: 2. |
|
363 |
|
364 @item suppress_verbose_help_message |
|
365 @xref{Getting Help}. |
|
366 |
|
367 Default value: 1. |
|
368 |
|
369 @item warn_assign_as_truth_value |
|
370 @xref{The if Statement}. |
|
371 |
|
372 Default value: 1. |
|
373 |
|
374 @item warn_comma_in_global_decl |
|
375 @xref{Global Variables}. |
|
376 |
|
377 Default value: 1. |
|
378 |
|
379 @item warn_divide_by_zero |
|
380 @xref{Arithmetic Ops}. |
|
381 |
|
382 Default value: 1. |
|
383 |
4460
|
384 @item warn_empty_list_elements |
|
385 @xref{Empty Matrices}. |
|
386 |
|
387 Default value: 0. |
|
388 |
4455
|
389 @item warn_fortran_indexing |
|
390 @xref{Index Expressions}. |
|
391 |
|
392 Default value: 0. |
|
393 |
4452
|
394 @item warn_function_name_clash |
|
395 @xref{Function Files}. |
|
396 |
|
397 Default value: 1. |
|
398 |
4451
|
399 @item warn_imag_to_real |
|
400 @xref{Special Utility Matrices}. |
|
401 |
|
402 Default value: 0. |
|
403 |
4452
|
404 @item warn_missing_semicolon |
|
405 @xref{Defining Functions}. |
|
406 |
|
407 Default value: 0. |
|
408 |
4457
|
409 @item warn_neg_dim_as_zero |
|
410 @xref{Special Utility Matrices}. |
|
411 |
|
412 Default value: 0. |
|
413 |
4452
|
414 @item warn_num_to_str |
|
415 @xref{String Conversions}. |
3294
|
416 |
|
417 Default value: 1. |
|
418 |
4452
|
419 @item warn_str_to_num |
|
420 @xref{String Conversions}. |
|
421 |
|
422 Default value: 0. |
|
423 |
3294
|
424 @item warn_reload_forces_clear |
|
425 @xref{Dynamically Linked Functions}. |
|
426 |
|
427 Default value: 1. |
|
428 |
4461
|
429 @item warn_resize_on_range_error |
|
430 @xref{Index Expressions}. |
|
431 |
|
432 Default value: 0. |
|
433 |
4452
|
434 @item warn_single_quote_string) |
|
435 @xref{String Conversions}. |
|
436 |
|
437 Default value: 0. |
|
438 |
3294
|
439 @item warn_variable_switch_label |
|
440 @xref{The switch Statement}. |
|
441 |
|
442 Default value: 0. |
|
443 |
|
444 @item whitespace_in_literal_matrix |
|
445 @xref{Matrices}. |
|
446 |
|
447 Default value: @code{""}. |
|
448 @end vtable |
|
449 |
|
450 |
4167
|
451 @node Defaults from the Environment |
3294
|
452 @section Defaults from the Environment |
|
453 |
|
454 Octave uses the values of the following environment variables to set the |
|
455 default values for the corresponding built-in variables. In addition, |
|
456 the values from the environment may be overridden by command-line |
|
457 arguments. @xref{Command Line Options}. |
|
458 |
|
459 @vtable @code |
|
460 @item EDITOR |
|
461 @xref{Commands For History}. |
|
462 |
|
463 Built-in variable: @code{EDITOR}. |
|
464 |
|
465 @item OCTAVE_EXEC_PATH |
|
466 @xref{Controlling Subprocesses}. |
|
467 |
|
468 Built-in variable: @code{EXEC_PATH}. |
|
469 Command-line argument: @code{--exec-path}. |
|
470 |
|
471 @item OCTAVE_PATH |
|
472 @xref{Function Files}. |
|
473 |
|
474 Built-in variable: @code{LOADPATH}. |
|
475 Command-line argument: @code{--path}. |
|
476 |
|
477 @item OCTAVE_INFO_FILE |
|
478 @xref{Getting Help}. |
|
479 |
|
480 Built-in variable: @code{INFO_FILE}. |
|
481 Command-line argument: @code{--info-file}. |
|
482 |
|
483 @item OCTAVE_INFO_PROGRAM |
|
484 @xref{Getting Help}. |
|
485 |
|
486 Built-in variable: @code{INFO_PROGRAM}. |
|
487 Command-line argument: @code{--info-program}. |
|
488 |
|
489 @item OCTAVE_HISTSIZE |
|
490 @xref{Commands For History}. |
|
491 |
|
492 Built-in variable: @code{history_size}. |
|
493 |
|
494 @item OCTAVE_HISTFILE |
|
495 @xref{Commands For History}. |
|
496 |
|
497 Built-in variable: @code{history_file}. |
|
498 @end vtable |