Mercurial > hg > octave-lyh
annotate doc/interpreter/var.txi @ 8258:2b408bbd8904
Add error bar series
author | David Bateman <dbateman@free.fr> |
---|---|
date | Wed, 22 Oct 2008 11:21:28 +0100 |
parents | bbaa5d7d0143 |
children | 83c59e3f3106 |
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 | |
186 | |
187 A variable that has been declared @dfn{persistent} within a function | |
188 will retain its contents in memory between subsequent calls to the | |
189 same function. The difference between persistent variables and global | |
190 variables is that persistent variables are local in scope to a | |
191 particular function and are not visible elsewhere. | |
192 | |
6899 | 193 The following example uses a persistent variable to create a function |
194 that prints the number of times it has been called. | |
195 | |
196 @example | |
197 @group | |
198 function count_calls () | |
199 persistent calls = 0; | |
7031 | 200 printf ("'count_calls' has been called %d times\n", |
201 ++calls); | |
6899 | 202 endfunction |
203 | |
204 for i = 1:3 | |
205 count_calls (); | |
206 endfor | |
207 | |
208 @print{} 'count_calls' has been called 1 times | |
209 @print{} 'count_calls' has been called 2 times | |
210 @print{} 'count_calls' has been called 3 times | |
211 @end group | |
212 @end example | |
213 | |
214 As the example shows, a variable may be declared persistent using a | |
215 @code{persistent} declaration statement. The following statements are | |
216 all persistent declarations. | |
4686 | 217 |
218 @example | |
219 @group | |
220 persistent a | |
221 persistent a b | |
222 persistent c = 2 | |
223 persistent d = 3 e f = 5 | |
224 @end group | |
225 @end example | |
226 | |
227 The behavior of persistent variables is equivalent to the behavior of | |
228 static variables in C. The command @code{static} in octave is also | |
6899 | 229 recognized and is equivalent to @code{persistent}. |
230 | |
231 Like global variables, a persistent variable may only be initialized once. | |
6896 | 232 For example, after executing the following code |
4686 | 233 |
234 @example | |
235 @group | |
236 persistent pvar = 1 | |
237 persistent pvar = 2 | |
238 @end group | |
239 @end example | |
240 | |
241 @noindent | |
6896 | 242 the value of the persistent variable @code{pvar} is 1, not 2. |
4686 | 243 |
6899 | 244 If a persistent variable is declared but not initialized to a specific |
245 value, it will contain an empty matrix. So, it is also possible to | |
246 initialize a persistent variable by checking whether it is empty, as the | |
247 following example illustrates. | |
248 | |
249 @example | |
250 @group | |
251 function count_calls () | |
252 persistent calls; | |
253 if (isempty (calls)) | |
254 calls = 0; | |
255 endif | |
7031 | 256 printf ("'count_calls' has been called %d times\n", |
257 ++calls); | |
6899 | 258 endfunction |
259 @end group | |
260 @end example | |
261 | |
262 @noindent | |
263 This implementation behaves in exactly the same way as the previous | |
264 implementation of @code{count_calls}. | |
265 | |
266 The value of a persistent variable is kept in memory until it is | |
267 explicitly cleared. Assuming that the implementation of @code{count_calls} | |
268 is saved on disc, we get the following behaviour. | |
269 | |
270 @example | |
271 @group | |
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 | |
278 clear | |
279 for i = 1:2 | |
280 count_calls(); | |
281 endfor | |
282 @print{} 'count_calls' has been called 3 times | |
283 @print{} 'count_calls' has been called 4 times | |
284 | |
285 clear all | |
286 for i = 1:2 | |
287 count_calls(); | |
288 endfor | |
289 @print{} 'count_calls' has been called 1 times | |
290 @print{} 'count_calls' has been called 2 times | |
291 | |
292 clear count_calls | |
293 for i = 1:2 | |
294 count_calls(); | |
295 endfor | |
296 @print{} 'count_calls' has been called 1 times | |
297 @print{} 'count_calls' has been called 2 times | |
298 @end group | |
299 @end example | |
300 | |
301 @noindent | |
302 That is, the persistent variable is only removed from memory when the | |
303 function containing the variable is removed. Note that if the function | |
304 definition is typed directly into the Octave prompt, the persistent | |
305 variable will be cleared by a simple @code{clear} command as the entire | |
306 function definition will be removed from memory. If you do not want | |
307 a persistent variable to be removed from memory even if the function is | |
308 cleared, you should use the @code{mlock} function as described in | |
309 @xref{Function Locking}. | |
310 | |
4167 | 311 @node Status of Variables |
3294 | 312 @section Status of Variables |
313 | |
6623 | 314 When creating simple one-shot programs it can be very convenient to |
315 see which variables are available at the prompt. The function @code{who} | |
316 and its siblings @code{whos} and @code{whos_line_format} will show | |
317 different information about what is in memory, as the following shows. | |
318 | |
319 @example | |
320 str = "A random string"; | |
321 who -variables | |
322 @print{} *** local user variables: | |
323 @print{} | |
324 @print{} __nargin__ str | |
325 @end example | |
3294 | 326 |
3361 | 327 @DOCSTRING(who) |
3294 | 328 |
4913 | 329 @DOCSTRING(whos) |
330 | |
331 @DOCSTRING(whos_line_format) | |
332 | |
6623 | 333 Instead of displaying which variables are in memory, it is possible |
334 to determine if a given variable is available. That way it is possible | |
335 to alter the behaviour of a program depending on the existence of a | |
336 variable. The following example illustrates this. | |
337 | |
338 @example | |
339 if (! exist ("meaning", "var")) | |
340 disp ("The program has no 'meaning'"); | |
341 endif | |
342 @end example | |
343 | |
3361 | 344 @DOCSTRING(exist) |
3294 | 345 |
6623 | 346 Usually Octave will manage the memory, but sometimes it can be practical |
347 to remove variables from memory manually. This is usually needed when | |
348 working with large variables that fill a substantial part of the memory. | |
349 On a computer that uses the IEEE floating point format, the following | |
350 program allocates a matrix that requires around 128 MB memory. | |
351 | |
352 @example | |
353 large_matrix = zeros (4000, 4000); | |
354 @end example | |
355 | |
356 @noindent | |
357 Since having this variable in memory might slow down other computations, | |
358 it can be necessary to remove it manually from memory. The @code{clear} | |
359 function allows this. | |
360 | |
361 @DOCSTRING(clear) | |
362 | |
363 Information about a function or variable such as it's location in the | |
364 file system can also be acquired from within Octave. This is usually | |
365 only useful during development of programs, and not within a program. | |
366 | |
3361 | 367 @DOCSTRING(document) |
3294 | 368 |
3361 | 369 @DOCSTRING(type) |
3294 | 370 |
3361 | 371 @DOCSTRING(which) |
3294 | 372 |
4167 | 373 @node Summary of Built-in Variables |
3294 | 374 @section Summary of Built-in Variables |
375 | |
376 Here is a summary of all of Octave's built-in variables along with | |
377 cross references to additional information and their default values. In | |
378 the following table @var{octave-home} stands for the root directory | |
379 where all of Octave is installed (the default is @file{@value{OCTAVEHOME}}, | |
380 @var{version} stands for the Octave version number (for example, | |
381 @value{VERSION}) and @var{arch} stands for the type of system for which | |
5942 | 382 Octave was compiled (for example, @code{x86_64-unknown-linux-gnu}). |
3294 | 383 |
384 @vtable @code | |
385 @item EDITOR | |
386 @xref{Commands For History}. | |
387 | |
388 Default value: @code{"emacs"}. | |
389 | |
390 @item EXEC_PATH | |
391 @xref{Controlling Subprocesses}. | |
392 | |
393 Default value: @code{":$PATH"}. | |
394 | |
395 @item OCTAVE_HOME | |
396 | |
397 Default value: @code{"@value{OCTAVEHOME}"}. | |
398 | |
399 @item PAGER | |
400 @xref{Input and Output}. | |
401 | |
402 Default value: @code{"less", or "more"}. | |
403 | |
404 @item PS1 | |
405 @xref{Customizing the Prompt}. | |
406 | |
407 Default value: @code{"\s:\#> "}. | |
408 | |
409 @item PS2 | |
410 @xref{Customizing the Prompt}. | |
411 | |
412 Default value: @code{"> "}. | |
413 | |
414 @item PS4 | |
415 @xref{Customizing the Prompt}. | |
416 | |
417 Default value: @code{"+ "}. | |
418 | |
419 @item beep_on_error | |
6667 | 420 @xref{Errors and Warnings}. |
3294 | 421 |
422 Default value: 0. | |
423 | |
424 @item completion_append_char | |
425 @xref{Commands For Completion}. | |
426 | |
427 Default value: @code{" "}. | |
428 | |
5287 | 429 @item default_save_options |
3294 | 430 @xref{Simple File I/O}. |
431 | |
432 Default value: @code{"ascii"}. | |
433 | |
434 @item crash_dumps_octave_core | |
435 @xref{Simple File I/O}. | |
436 | |
437 Default value: 1. | |
438 | |
439 @item fixed_point_format | |
440 @xref{Matrices}. | |
441 | |
442 Default value: 0. | |
443 | |
444 @item gnuplot_binary | |
445 @xref{Three-Dimensional Plotting}. | |
446 | |
447 Default value: @code{"gnuplot"}. | |
448 | |
449 @item history_file | |
450 @xref{Commands For History}. | |
451 | |
452 Default value: @code{"~/.octave_hist"}. | |
453 | |
454 @item history_size | |
455 @xref{Commands For History}. | |
456 | |
457 Default value: 1024. | |
458 | |
459 @item ignore_function_time_stamp | |
460 @xref{Function Files}. | |
461 | |
462 Default value: @code{"system"}. | |
463 | |
464 @item max_recursion_depth | |
465 @xref{Recursion}. | |
466 | |
467 Default value: 256. | |
468 | |
469 @item output_max_field_width | |
470 @xref{Matrices}. | |
471 | |
472 Default value: 10. | |
473 | |
474 @item output_precision | |
475 @xref{Matrices}. | |
476 | |
477 Default value: 5. | |
478 | |
479 @item page_screen_output | |
480 @xref{Input and Output}. | |
481 | |
482 Default value: 1. | |
483 | |
484 @item print_empty_dimensions | |
485 @xref{Empty Matrices}. | |
486 | |
487 Default value: 1. | |
488 | |
489 @item return_last_computed_value | |
490 @xref{Returning From a Function}. | |
491 | |
492 Default value: 0. | |
493 | |
494 @item save_precision | |
495 @xref{Simple File I/O}. | |
496 | |
497 Default value: 17. | |
498 | |
499 @item saving_history | |
500 @xref{Commands For History}. | |
501 | |
502 Default value: 1. | |
503 | |
4449 | 504 @item sighup_dumps_octave_core |
505 @xref{Simple File I/O}. | |
506 | |
507 Default value: 1. | |
508 | |
509 @item sigterm_dumps_octave_core | |
510 @xref{Simple File I/O}. | |
511 | |
512 Default value: 1. | |
513 | |
3294 | 514 @item silent_functions |
515 @xref{Defining Functions}. | |
516 | |
517 Default value: 0. | |
518 | |
519 @item split_long_rows | |
520 @xref{Matrices}. | |
521 | |
522 Default value: 1. | |
523 | |
524 @item struct_levels_to_print | |
525 @xref{Data Structures}. | |
526 | |
527 Default value: 2. | |
528 | |
529 @item suppress_verbose_help_message | |
530 @xref{Getting Help}. | |
531 | |
532 Default value: 1. | |
533 @end vtable | |
534 | |
535 | |
4167 | 536 @node Defaults from the Environment |
3294 | 537 @section Defaults from the Environment |
538 | |
539 Octave uses the values of the following environment variables to set the | |
6477 | 540 default values for the corresponding built-in or internal variables. |
541 In addition, the values from the environment may be overridden by | |
542 command-line arguments. @xref{Command Line Options}. | |
3294 | 543 |
544 @vtable @code | |
545 @item EDITOR | |
546 @xref{Commands For History}. | |
547 | |
548 Built-in variable: @code{EDITOR}. | |
549 | |
550 @item OCTAVE_EXEC_PATH | |
551 @xref{Controlling Subprocesses}. | |
552 | |
553 Built-in variable: @code{EXEC_PATH}. | |
554 Command-line argument: @code{--exec-path}. | |
555 | |
556 @item OCTAVE_PATH | |
557 @xref{Function Files}. | |
558 | |
6477 | 559 Internal variable changed by function @code{path}. |
3294 | 560 Command-line argument: @code{--path}. |
561 | |
562 @item OCTAVE_INFO_FILE | |
563 @xref{Getting Help}. | |
564 | |
6477 | 565 Internal variable changed by function @code{info_file}. |
3294 | 566 Command-line argument: @code{--info-file}. |
567 | |
568 @item OCTAVE_INFO_PROGRAM | |
569 @xref{Getting Help}. | |
570 | |
6477 | 571 Internal variable changed by function @code{info_program}. |
3294 | 572 Command-line argument: @code{--info-program}. |
573 | |
574 @item OCTAVE_HISTSIZE | |
575 @xref{Commands For History}. | |
576 | |
577 Built-in variable: @code{history_size}. | |
578 | |
579 @item OCTAVE_HISTFILE | |
580 @xref{Commands For History}. | |
581 | |
582 Built-in variable: @code{history_file}. | |
583 @end vtable |