6778
|
1 @c Copyright (C) 1996, 1997, 2007 John W. Eaton |
3294
|
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 |
6550
|
56 @DOCSTRING(isvarname) |
|
57 |
3294
|
58 @menu |
|
59 * Global Variables:: |
4686
|
60 * Persistent Variables:: |
3294
|
61 * Status of Variables:: |
|
62 * Summary of Built-in Variables:: |
|
63 * Defaults from the Environment:: |
|
64 @end menu |
|
65 |
4167
|
66 @node Global Variables |
3294
|
67 @section Global Variables |
|
68 @cindex global variables |
|
69 @cindex @code{global} statement |
|
70 @cindex variables, global |
|
71 |
|
72 A variable that has been declared @dfn{global} may be accessed from |
|
73 within a function body without having to pass it as a formal parameter. |
|
74 |
|
75 A variable may be declared global using a @code{global} declaration |
|
76 statement. The following statements are all global declarations. |
|
77 |
|
78 @example |
|
79 @group |
|
80 global a |
4504
|
81 global a b |
|
82 global c = 2 |
|
83 global d = 3 e f = 5 |
3294
|
84 @end group |
|
85 @end example |
|
86 |
4504
|
87 A global variable may only be initialized once in a @code{global} |
|
88 statement. For example, after executing the following code |
|
89 |
|
90 @example |
|
91 @group |
|
92 global gvar = 1 |
|
93 global gvar = 2 |
|
94 @end group |
|
95 @end example |
|
96 |
|
97 @noindent |
6077
|
98 the value of the global variable @code{gvar} is 1, not 2. Issuing a |
6623
|
99 @samp{clear gvar} command does not change the above behavior, but |
6077
|
100 @samp{clear all} does. |
4504
|
101 |
3294
|
102 It is necessary declare a variable as global within a function body in |
|
103 order to access it. For example, |
|
104 |
|
105 @example |
|
106 @group |
|
107 global x |
|
108 function f () |
|
109 x = 1; |
|
110 endfunction |
|
111 f () |
|
112 @end group |
|
113 @end example |
|
114 |
|
115 @noindent |
|
116 does @emph{not} set the value of the global variable @code{x} to 1. In |
|
117 order to change the value of the global variable @code{x}, you must also |
|
118 declare it to be global within the function body, like this |
|
119 |
|
120 @example |
|
121 @group |
|
122 function f () |
|
123 global x; |
|
124 x = 1; |
|
125 endfunction |
|
126 @end group |
|
127 @end example |
|
128 |
|
129 Passing a global variable in a function parameter list will |
|
130 make a local copy and not modify the global value. For example, given |
|
131 the function |
|
132 |
|
133 @example |
|
134 @group |
|
135 function f (x) |
|
136 x = 0 |
|
137 endfunction |
|
138 @end group |
|
139 @end example |
|
140 |
|
141 @noindent |
|
142 and the definition of @code{x} as a global variable at the top level, |
|
143 |
|
144 @example |
|
145 global x = 13 |
|
146 @end example |
|
147 |
|
148 @noindent |
|
149 the expression |
|
150 |
|
151 @example |
|
152 f (x) |
|
153 @end example |
|
154 |
|
155 @noindent |
|
156 will display the value of @code{x} from inside the function as 0, |
|
157 but the value of @code{x} at the top level remains unchanged, because |
|
158 the function works with a @emph{copy} of its argument. |
|
159 |
4029
|
160 @DOCSTRING(isglobal) |
3294
|
161 |
4686
|
162 @node Persistent Variables |
|
163 @section Persistent Variables |
|
164 @cindex persistent variables |
|
165 @cindex @code{persistent} statement |
|
166 @cindex variables, persistent |
|
167 |
|
168 A variable that has been declared @dfn{persistent} within a function |
|
169 will retain its contents in memory between subsequent calls to the |
|
170 same function. The difference between persistent variables and global |
|
171 variables is that persistent variables are local in scope to a |
|
172 particular function and are not visible elsewhere. |
|
173 |
6899
|
174 The following example uses a persistent variable to create a function |
|
175 that prints the number of times it has been called. |
|
176 |
|
177 @example |
|
178 @group |
|
179 function count_calls () |
|
180 persistent calls = 0; |
|
181 printf ("'count_calls' has been called %d times\n", ++calls); |
|
182 endfunction |
|
183 |
|
184 for i = 1:3 |
|
185 count_calls (); |
|
186 endfor |
|
187 |
|
188 @print{} 'count_calls' has been called 1 times |
|
189 @print{} 'count_calls' has been called 2 times |
|
190 @print{} 'count_calls' has been called 3 times |
|
191 @end group |
|
192 @end example |
|
193 |
|
194 As the example shows, a variable may be declared persistent using a |
|
195 @code{persistent} declaration statement. The following statements are |
|
196 all persistent declarations. |
4686
|
197 |
|
198 @example |
|
199 @group |
|
200 persistent a |
|
201 persistent a b |
|
202 persistent c = 2 |
|
203 persistent d = 3 e f = 5 |
|
204 @end group |
|
205 @end example |
|
206 |
|
207 The behavior of persistent variables is equivalent to the behavior of |
|
208 static variables in C. The command @code{static} in octave is also |
6899
|
209 recognized and is equivalent to @code{persistent}. |
|
210 |
|
211 Like global variables, a persistent variable may only be initialized once. |
6896
|
212 For example, after executing the following code |
4686
|
213 |
|
214 @example |
|
215 @group |
|
216 persistent pvar = 1 |
|
217 persistent pvar = 2 |
|
218 @end group |
|
219 @end example |
|
220 |
|
221 @noindent |
6896
|
222 the value of the persistent variable @code{pvar} is 1, not 2. |
4686
|
223 |
6899
|
224 If a persistent variable is declared but not initialized to a specific |
|
225 value, it will contain an empty matrix. So, it is also possible to |
|
226 initialize a persistent variable by checking whether it is empty, as the |
|
227 following example illustrates. |
|
228 |
|
229 @example |
|
230 @group |
|
231 function count_calls () |
|
232 persistent calls; |
|
233 if (isempty (calls)) |
|
234 calls = 0; |
|
235 endif |
|
236 printf ("'count_calls' has been called %d times\n", ++calls); |
|
237 endfunction |
|
238 @end group |
|
239 @end example |
|
240 |
|
241 @noindent |
|
242 This implementation behaves in exactly the same way as the previous |
|
243 implementation of @code{count_calls}. |
|
244 |
|
245 The value of a persistent variable is kept in memory until it is |
|
246 explicitly cleared. Assuming that the implementation of @code{count_calls} |
|
247 is saved on disc, we get the following behaviour. |
|
248 |
|
249 @example |
|
250 @group |
|
251 for i = 1:2 |
|
252 count_calls (); |
|
253 endfor |
|
254 @print{} 'count_calls' has been called 1 times |
|
255 @print{} 'count_calls' has been called 2 times |
|
256 |
|
257 clear |
|
258 for i = 1:2 |
|
259 count_calls(); |
|
260 endfor |
|
261 @print{} 'count_calls' has been called 3 times |
|
262 @print{} 'count_calls' has been called 4 times |
|
263 |
|
264 clear all |
|
265 for i = 1:2 |
|
266 count_calls(); |
|
267 endfor |
|
268 @print{} 'count_calls' has been called 1 times |
|
269 @print{} 'count_calls' has been called 2 times |
|
270 |
|
271 clear count_calls |
|
272 for i = 1:2 |
|
273 count_calls(); |
|
274 endfor |
|
275 @print{} 'count_calls' has been called 1 times |
|
276 @print{} 'count_calls' has been called 2 times |
|
277 @end group |
|
278 @end example |
|
279 |
|
280 @noindent |
|
281 That is, the persistent variable is only removed from memory when the |
|
282 function containing the variable is removed. Note that if the function |
|
283 definition is typed directly into the Octave prompt, the persistent |
|
284 variable will be cleared by a simple @code{clear} command as the entire |
|
285 function definition will be removed from memory. If you do not want |
|
286 a persistent variable to be removed from memory even if the function is |
|
287 cleared, you should use the @code{mlock} function as described in |
|
288 @xref{Function Locking}. |
|
289 |
4167
|
290 @node Status of Variables |
3294
|
291 @section Status of Variables |
|
292 |
6623
|
293 When creating simple one-shot programs it can be very convenient to |
|
294 see which variables are available at the prompt. The function @code{who} |
|
295 and its siblings @code{whos} and @code{whos_line_format} will show |
|
296 different information about what is in memory, as the following shows. |
|
297 |
|
298 @example |
|
299 str = "A random string"; |
|
300 who -variables |
|
301 @print{} *** local user variables: |
|
302 @print{} |
|
303 @print{} __nargin__ str |
|
304 @end example |
3294
|
305 |
3361
|
306 @DOCSTRING(who) |
3294
|
307 |
4913
|
308 @DOCSTRING(whos) |
|
309 |
|
310 @DOCSTRING(whos_line_format) |
|
311 |
6623
|
312 Instead of displaying which variables are in memory, it is possible |
|
313 to determine if a given variable is available. That way it is possible |
|
314 to alter the behaviour of a program depending on the existence of a |
|
315 variable. The following example illustrates this. |
|
316 |
|
317 @example |
|
318 if (! exist ("meaning", "var")) |
|
319 disp ("The program has no 'meaning'"); |
|
320 endif |
|
321 @end example |
|
322 |
3361
|
323 @DOCSTRING(exist) |
3294
|
324 |
6623
|
325 Usually Octave will manage the memory, but sometimes it can be practical |
|
326 to remove variables from memory manually. This is usually needed when |
|
327 working with large variables that fill a substantial part of the memory. |
|
328 On a computer that uses the IEEE floating point format, the following |
|
329 program allocates a matrix that requires around 128 MB memory. |
|
330 |
|
331 @example |
|
332 large_matrix = zeros (4000, 4000); |
|
333 @end example |
|
334 |
|
335 @noindent |
|
336 Since having this variable in memory might slow down other computations, |
|
337 it can be necessary to remove it manually from memory. The @code{clear} |
|
338 function allows this. |
|
339 |
|
340 @DOCSTRING(clear) |
|
341 |
|
342 Information about a function or variable such as it's location in the |
|
343 file system can also be acquired from within Octave. This is usually |
|
344 only useful during development of programs, and not within a program. |
|
345 |
3361
|
346 @DOCSTRING(document) |
3294
|
347 |
3361
|
348 @DOCSTRING(type) |
3294
|
349 |
3361
|
350 @DOCSTRING(which) |
3294
|
351 |
4167
|
352 @node Summary of Built-in Variables |
3294
|
353 @section Summary of Built-in Variables |
|
354 |
|
355 Here is a summary of all of Octave's built-in variables along with |
|
356 cross references to additional information and their default values. In |
|
357 the following table @var{octave-home} stands for the root directory |
|
358 where all of Octave is installed (the default is @file{@value{OCTAVEHOME}}, |
|
359 @var{version} stands for the Octave version number (for example, |
|
360 @value{VERSION}) and @var{arch} stands for the type of system for which |
5942
|
361 Octave was compiled (for example, @code{x86_64-unknown-linux-gnu}). |
3294
|
362 |
|
363 @vtable @code |
|
364 @item EDITOR |
|
365 @xref{Commands For History}. |
|
366 |
|
367 Default value: @code{"emacs"}. |
|
368 |
|
369 @item EXEC_PATH |
|
370 @xref{Controlling Subprocesses}. |
|
371 |
|
372 Default value: @code{":$PATH"}. |
|
373 |
|
374 @item OCTAVE_HOME |
|
375 |
|
376 Default value: @code{"@value{OCTAVEHOME}"}. |
|
377 |
|
378 @item PAGER |
|
379 @xref{Input and Output}. |
|
380 |
|
381 Default value: @code{"less", or "more"}. |
|
382 |
|
383 @item PS1 |
|
384 @xref{Customizing the Prompt}. |
|
385 |
|
386 Default value: @code{"\s:\#> "}. |
|
387 |
|
388 @item PS2 |
|
389 @xref{Customizing the Prompt}. |
|
390 |
|
391 Default value: @code{"> "}. |
|
392 |
|
393 @item PS4 |
|
394 @xref{Customizing the Prompt}. |
|
395 |
|
396 Default value: @code{"+ "}. |
|
397 |
|
398 @item beep_on_error |
6667
|
399 @xref{Errors and Warnings}. |
3294
|
400 |
|
401 Default value: 0. |
|
402 |
|
403 @item completion_append_char |
|
404 @xref{Commands For Completion}. |
|
405 |
|
406 Default value: @code{" "}. |
|
407 |
5287
|
408 @item default_save_options |
3294
|
409 @xref{Simple File I/O}. |
|
410 |
|
411 Default value: @code{"ascii"}. |
|
412 |
|
413 @item crash_dumps_octave_core |
|
414 @xref{Simple File I/O}. |
|
415 |
|
416 Default value: 1. |
|
417 |
|
418 @item fixed_point_format |
|
419 @xref{Matrices}. |
|
420 |
|
421 Default value: 0. |
|
422 |
|
423 @item gnuplot_binary |
|
424 @xref{Three-Dimensional Plotting}. |
|
425 |
|
426 Default value: @code{"gnuplot"}. |
|
427 |
|
428 @item history_file |
|
429 @xref{Commands For History}. |
|
430 |
|
431 Default value: @code{"~/.octave_hist"}. |
|
432 |
|
433 @item history_size |
|
434 @xref{Commands For History}. |
|
435 |
|
436 Default value: 1024. |
|
437 |
|
438 @item ignore_function_time_stamp |
|
439 @xref{Function Files}. |
|
440 |
|
441 Default value: @code{"system"}. |
|
442 |
|
443 @item max_recursion_depth |
|
444 @xref{Recursion}. |
|
445 |
|
446 Default value: 256. |
|
447 |
|
448 @item output_max_field_width |
|
449 @xref{Matrices}. |
|
450 |
|
451 Default value: 10. |
|
452 |
|
453 @item output_precision |
|
454 @xref{Matrices}. |
|
455 |
|
456 Default value: 5. |
|
457 |
|
458 @item page_screen_output |
|
459 @xref{Input and Output}. |
|
460 |
|
461 Default value: 1. |
|
462 |
|
463 @item print_answer_id_name |
|
464 @xref{Terminal Output}. |
|
465 |
|
466 Default value: 1. |
|
467 |
|
468 @item print_empty_dimensions |
|
469 @xref{Empty Matrices}. |
|
470 |
|
471 Default value: 1. |
|
472 |
|
473 @item return_last_computed_value |
|
474 @xref{Returning From a Function}. |
|
475 |
|
476 Default value: 0. |
|
477 |
|
478 @item save_precision |
|
479 @xref{Simple File I/O}. |
|
480 |
|
481 Default value: 17. |
|
482 |
|
483 @item saving_history |
|
484 @xref{Commands For History}. |
|
485 |
|
486 Default value: 1. |
|
487 |
4449
|
488 @item sighup_dumps_octave_core |
|
489 @xref{Simple File I/O}. |
|
490 |
|
491 Default value: 1. |
|
492 |
|
493 @item sigterm_dumps_octave_core |
|
494 @xref{Simple File I/O}. |
|
495 |
|
496 Default value: 1. |
|
497 |
3294
|
498 @item silent_functions |
|
499 @xref{Defining Functions}. |
|
500 |
|
501 Default value: 0. |
|
502 |
|
503 @item split_long_rows |
|
504 @xref{Matrices}. |
|
505 |
|
506 Default value: 1. |
|
507 |
|
508 @item struct_levels_to_print |
|
509 @xref{Data Structures}. |
|
510 |
|
511 Default value: 2. |
|
512 |
|
513 @item suppress_verbose_help_message |
|
514 @xref{Getting Help}. |
|
515 |
|
516 Default value: 1. |
|
517 @end vtable |
|
518 |
|
519 |
4167
|
520 @node Defaults from the Environment |
3294
|
521 @section Defaults from the Environment |
|
522 |
|
523 Octave uses the values of the following environment variables to set the |
6477
|
524 default values for the corresponding built-in or internal variables. |
|
525 In addition, the values from the environment may be overridden by |
|
526 command-line arguments. @xref{Command Line Options}. |
3294
|
527 |
|
528 @vtable @code |
|
529 @item EDITOR |
|
530 @xref{Commands For History}. |
|
531 |
|
532 Built-in variable: @code{EDITOR}. |
|
533 |
|
534 @item OCTAVE_EXEC_PATH |
|
535 @xref{Controlling Subprocesses}. |
|
536 |
|
537 Built-in variable: @code{EXEC_PATH}. |
|
538 Command-line argument: @code{--exec-path}. |
|
539 |
|
540 @item OCTAVE_PATH |
|
541 @xref{Function Files}. |
|
542 |
6477
|
543 Internal variable changed by function @code{path}. |
3294
|
544 Command-line argument: @code{--path}. |
|
545 |
|
546 @item OCTAVE_INFO_FILE |
|
547 @xref{Getting Help}. |
|
548 |
6477
|
549 Internal variable changed by function @code{info_file}. |
3294
|
550 Command-line argument: @code{--info-file}. |
|
551 |
|
552 @item OCTAVE_INFO_PROGRAM |
|
553 @xref{Getting Help}. |
|
554 |
6477
|
555 Internal variable changed by function @code{info_program}. |
3294
|
556 Command-line argument: @code{--info-program}. |
|
557 |
|
558 @item OCTAVE_HISTSIZE |
|
559 @xref{Commands For History}. |
|
560 |
|
561 Built-in variable: @code{history_size}. |
|
562 |
|
563 @item OCTAVE_HISTFILE |
|
564 @xref{Commands For History}. |
|
565 |
|
566 Built-in variable: @code{history_file}. |
|
567 @end vtable |