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