Mercurial > octave-nkf
annotate doc/interpreter/var.txi @ 11966:c45c78914af0 release-3-2-x
Various documentation improvements
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Sat, 06 Jun 2009 18:30:52 -0700 |
parents | 923c7cb7f13f |
children | 322f43e0e170 |
rev | line source |
---|---|
7018 | 1 @c Copyright (C) 1996, 1997, 1999, 2000, 2002, 2003, 2004, 2005, |
8920 | 2 @c 2006, 2007, 2008, 2009 John W. Eaton |
7018 | 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 | |
9037
4cb9f994dcec
Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
58 There is one built-in variable with a special meaning. The @code{ans} variable |
8566
da95767511f5
Remove descriptions of built-in variables from manual
sh@sh-laptop
parents:
8519
diff
changeset
|
59 always contains the result of the last computation, where the output wasn't |
9037
4cb9f994dcec
Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
60 assigned to any variable. The code @code{a = cos (pi)} will assign the value -1 |
4cb9f994dcec
Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
61 to the variable @code{a}, but will not change the value of @code{ans}. However, |
8566
da95767511f5
Remove descriptions of built-in variables from manual
sh@sh-laptop
parents:
8519
diff
changeset
|
62 the code @code{cos (pi)} will set the value of @code{ans} to -1. |
3294 | 63 |
64 Variables in Octave do not have fixed types, so it is possible to first | |
65 store a numeric value in a variable and then to later use the same name | |
66 to hold a string value in the same program. Variables may not be used | |
67 before they have been given a value. Doing so results in an error. | |
68 | |
8567 | 69 @cindex @code{ans} |
8566
da95767511f5
Remove descriptions of built-in variables from manual
sh@sh-laptop
parents:
8519
diff
changeset
|
70 @DOCSTRING(ans) |
da95767511f5
Remove descriptions of built-in variables from manual
sh@sh-laptop
parents:
8519
diff
changeset
|
71 |
6550 | 72 @DOCSTRING(isvarname) |
73 | |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
74 @DOCSTRING(genvarname) |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
75 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
76 @DOCSTRING(namelengthmax) |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
77 |
3294 | 78 @menu |
79 * Global Variables:: | |
4686 | 80 * Persistent Variables:: |
3294 | 81 * Status of Variables:: |
82 @end menu | |
83 | |
4167 | 84 @node Global Variables |
3294 | 85 @section Global Variables |
86 @cindex global variables | |
87 @cindex @code{global} statement | |
88 @cindex variables, global | |
89 | |
90 A variable that has been declared @dfn{global} may be accessed from | |
91 within a function body without having to pass it as a formal parameter. | |
92 | |
93 A variable may be declared global using a @code{global} declaration | |
94 statement. The following statements are all global declarations. | |
95 | |
96 @example | |
97 @group | |
98 global a | |
4504 | 99 global a b |
100 global c = 2 | |
101 global d = 3 e f = 5 | |
3294 | 102 @end group |
103 @end example | |
104 | |
4504 | 105 A global variable may only be initialized once in a @code{global} |
106 statement. For example, after executing the following code | |
107 | |
108 @example | |
109 @group | |
110 global gvar = 1 | |
111 global gvar = 2 | |
112 @end group | |
113 @end example | |
114 | |
115 @noindent | |
6077 | 116 the value of the global variable @code{gvar} is 1, not 2. Issuing a |
6623 | 117 @samp{clear gvar} command does not change the above behavior, but |
6077 | 118 @samp{clear all} does. |
4504 | 119 |
3294 | 120 It is necessary declare a variable as global within a function body in |
121 order to access it. For example, | |
122 | |
123 @example | |
124 @group | |
125 global x | |
126 function f () | |
127 x = 1; | |
128 endfunction | |
129 f () | |
130 @end group | |
131 @end example | |
132 | |
133 @noindent | |
134 does @emph{not} set the value of the global variable @code{x} to 1. In | |
135 order to change the value of the global variable @code{x}, you must also | |
136 declare it to be global within the function body, like this | |
137 | |
138 @example | |
139 @group | |
140 function f () | |
141 global x; | |
142 x = 1; | |
143 endfunction | |
144 @end group | |
145 @end example | |
146 | |
147 Passing a global variable in a function parameter list will | |
148 make a local copy and not modify the global value. For example, given | |
149 the function | |
150 | |
151 @example | |
152 @group | |
153 function f (x) | |
154 x = 0 | |
155 endfunction | |
156 @end group | |
157 @end example | |
158 | |
159 @noindent | |
160 and the definition of @code{x} as a global variable at the top level, | |
161 | |
162 @example | |
163 global x = 13 | |
164 @end example | |
165 | |
166 @noindent | |
167 the expression | |
168 | |
169 @example | |
170 f (x) | |
171 @end example | |
172 | |
173 @noindent | |
174 will display the value of @code{x} from inside the function as 0, | |
175 but the value of @code{x} at the top level remains unchanged, because | |
176 the function works with a @emph{copy} of its argument. | |
177 | |
4029 | 178 @DOCSTRING(isglobal) |
3294 | 179 |
4686 | 180 @node Persistent Variables |
181 @section Persistent Variables | |
182 @cindex persistent variables | |
183 @cindex @code{persistent} statement | |
184 @cindex variables, persistent | |
8286
6f2d95255911
fix @seealso references to point to existing anchors
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8281
diff
changeset
|
185 @anchor{doc-persistent} |
4686 | 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 | |
9037
4cb9f994dcec
Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
189 same function. The difference between persistent variables and global |
4686 | 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 | |
9037
4cb9f994dcec
Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
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} | |
11966
c45c78914af0
Various documentation improvements
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
268 is saved on disk, we get the following behavior. |
6899 | 269 |
270 @example | |
271 for i = 1:2 | |
272 count_calls (); | |
273 endfor | |
274 @print{} 'count_calls' has been called 1 times | |
275 @print{} 'count_calls' has been called 2 times | |
276 | |
277 clear | |
278 for i = 1:2 | |
279 count_calls(); | |
280 endfor | |
281 @print{} 'count_calls' has been called 3 times | |
282 @print{} 'count_calls' has been called 4 times | |
283 | |
284 clear all | |
285 for i = 1:2 | |
286 count_calls(); | |
287 endfor | |
288 @print{} 'count_calls' has been called 1 times | |
289 @print{} 'count_calls' has been called 2 times | |
290 | |
291 clear count_calls | |
292 for i = 1:2 | |
293 count_calls(); | |
294 endfor | |
295 @print{} 'count_calls' has been called 1 times | |
296 @print{} 'count_calls' has been called 2 times | |
297 @end example | |
298 | |
299 @noindent | |
300 That is, the persistent variable is only removed from memory when the | |
301 function containing the variable is removed. Note that if the function | |
302 definition is typed directly into the Octave prompt, the persistent | |
303 variable will be cleared by a simple @code{clear} command as the entire | |
304 function definition will be removed from memory. If you do not want | |
305 a persistent variable to be removed from memory even if the function is | |
306 cleared, you should use the @code{mlock} function as described in | |
307 @xref{Function Locking}. | |
308 | |
4167 | 309 @node Status of Variables |
3294 | 310 @section Status of Variables |
311 | |
6623 | 312 When creating simple one-shot programs it can be very convenient to |
9037
4cb9f994dcec
Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
313 see which variables are available at the prompt. The function @code{who} |
6623 | 314 and its siblings @code{whos} and @code{whos_line_format} will show |
315 different information about what is in memory, as the following shows. | |
316 | |
317 @example | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9037
diff
changeset
|
318 @group |
6623 | 319 str = "A random string"; |
320 who -variables | |
321 @print{} *** local user variables: | |
322 @print{} | |
323 @print{} __nargin__ str | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9037
diff
changeset
|
324 @end group |
6623 | 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 |
9037
4cb9f994dcec
Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
334 to determine if a given variable is available. That way it is possible |
4cb9f994dcec
Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
335 to alter the behavior of a program depending on the existence of a |
4cb9f994dcec
Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
336 variable. The following example illustrates this. |
6623 | 337 |
338 @example | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9037
diff
changeset
|
339 @group |
6623 | 340 if (! exist ("meaning", "var")) |
341 disp ("The program has no 'meaning'"); | |
342 endif | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9037
diff
changeset
|
343 @end group |
6623 | 344 @end example |
345 | |
3361 | 346 @DOCSTRING(exist) |
3294 | 347 |
6623 | 348 Usually Octave will manage the memory, but sometimes it can be practical |
9037
4cb9f994dcec
Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
349 to remove variables from memory manually. This is usually needed when |
6623 | 350 working with large variables that fill a substantial part of the memory. |
351 On a computer that uses the IEEE floating point format, the following | |
352 program allocates a matrix that requires around 128 MB memory. | |
353 | |
354 @example | |
355 large_matrix = zeros (4000, 4000); | |
356 @end example | |
357 | |
358 @noindent | |
359 Since having this variable in memory might slow down other computations, | |
9037
4cb9f994dcec
Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
360 it can be necessary to remove it manually from memory. The @code{clear} |
6623 | 361 function allows this. |
362 | |
363 @DOCSTRING(clear) | |
364 | |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
8286
diff
changeset
|
365 Information about a function or variable such as its location in the |
9037
4cb9f994dcec
Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
366 file system can also be acquired from within Octave. This is usually |
6623 | 367 only useful during development of programs, and not within a program. |
368 | |
3361 | 369 @DOCSTRING(type) |
3294 | 370 |
3361 | 371 @DOCSTRING(which) |
3294 | 372 |
8817
03b7f618ab3d
include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents:
8571
diff
changeset
|
373 @DOCSTRING(what) |