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