Mercurial > octave-antonio
annotate doc/interpreter/var.txi @ 8567:674d00f5e072
remove variable index
author | Soren Hauberg <soren@hauberg.org> |
---|---|
date | Thu, 22 Jan 2009 11:11:16 -0500 |
parents | da95767511f5 |
children | 38ee7ce3bc7d |
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 | |
8566
da95767511f5
Remove descriptions of built-in variables from manual
sh@sh-laptop
parents:
8519
diff
changeset
|
58 There is one built-in variable with a special meaning. The @code{ans} variable |
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 |
da95767511f5
Remove descriptions of built-in variables from manual
sh@sh-laptop
parents:
8519
diff
changeset
|
60 assigned to any variable. The code @code{a = cos (pi)} will assign the value -1 |
da95767511f5
Remove descriptions of built-in variables from manual
sh@sh-laptop
parents:
8519
diff
changeset
|
61 to the variable @code{a}, but will not change the value of @code{ans}. However, |
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 * Summary of Built-in Variables:: | |
83 * Defaults from the Environment:: | |
84 @end menu | |
85 | |
4167 | 86 @node Global Variables |
3294 | 87 @section Global Variables |
88 @cindex global variables | |
89 @cindex @code{global} statement | |
90 @cindex variables, global | |
91 | |
92 A variable that has been declared @dfn{global} may be accessed from | |
93 within a function body without having to pass it as a formal parameter. | |
94 | |
95 A variable may be declared global using a @code{global} declaration | |
96 statement. The following statements are all global declarations. | |
97 | |
98 @example | |
99 @group | |
100 global a | |
4504 | 101 global a b |
102 global c = 2 | |
103 global d = 3 e f = 5 | |
3294 | 104 @end group |
105 @end example | |
106 | |
4504 | 107 A global variable may only be initialized once in a @code{global} |
108 statement. For example, after executing the following code | |
109 | |
110 @example | |
111 @group | |
112 global gvar = 1 | |
113 global gvar = 2 | |
114 @end group | |
115 @end example | |
116 | |
117 @noindent | |
6077 | 118 the value of the global variable @code{gvar} is 1, not 2. Issuing a |
6623 | 119 @samp{clear gvar} command does not change the above behavior, but |
6077 | 120 @samp{clear all} does. |
4504 | 121 |
3294 | 122 It is necessary declare a variable as global within a function body in |
123 order to access it. For example, | |
124 | |
125 @example | |
126 @group | |
127 global x | |
128 function f () | |
129 x = 1; | |
130 endfunction | |
131 f () | |
132 @end group | |
133 @end example | |
134 | |
135 @noindent | |
136 does @emph{not} set the value of the global variable @code{x} to 1. In | |
137 order to change the value of the global variable @code{x}, you must also | |
138 declare it to be global within the function body, like this | |
139 | |
140 @example | |
141 @group | |
142 function f () | |
143 global x; | |
144 x = 1; | |
145 endfunction | |
146 @end group | |
147 @end example | |
148 | |
149 Passing a global variable in a function parameter list will | |
150 make a local copy and not modify the global value. For example, given | |
151 the function | |
152 | |
153 @example | |
154 @group | |
155 function f (x) | |
156 x = 0 | |
157 endfunction | |
158 @end group | |
159 @end example | |
160 | |
161 @noindent | |
162 and the definition of @code{x} as a global variable at the top level, | |
163 | |
164 @example | |
165 global x = 13 | |
166 @end example | |
167 | |
168 @noindent | |
169 the expression | |
170 | |
171 @example | |
172 f (x) | |
173 @end example | |
174 | |
175 @noindent | |
176 will display the value of @code{x} from inside the function as 0, | |
177 but the value of @code{x} at the top level remains unchanged, because | |
178 the function works with a @emph{copy} of its argument. | |
179 | |
4029 | 180 @DOCSTRING(isglobal) |
3294 | 181 |
4686 | 182 @node Persistent Variables |
183 @section Persistent Variables | |
184 @cindex persistent variables | |
185 @cindex @code{persistent} statement | |
186 @cindex variables, persistent | |
8286
6f2d95255911
fix @seealso references to point to existing anchors
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8281
diff
changeset
|
187 @anchor{doc-persistent} |
4686 | 188 |
189 A variable that has been declared @dfn{persistent} within a function | |
190 will retain its contents in memory between subsequent calls to the | |
191 same function. The difference between persistent variables and global | |
192 variables is that persistent variables are local in scope to a | |
193 particular function and are not visible elsewhere. | |
194 | |
6899 | 195 The following example uses a persistent variable to create a function |
196 that prints the number of times it has been called. | |
197 | |
198 @example | |
199 @group | |
200 function count_calls () | |
201 persistent calls = 0; | |
7031 | 202 printf ("'count_calls' has been called %d times\n", |
203 ++calls); | |
6899 | 204 endfunction |
205 | |
206 for i = 1:3 | |
207 count_calls (); | |
208 endfor | |
209 | |
210 @print{} 'count_calls' has been called 1 times | |
211 @print{} 'count_calls' has been called 2 times | |
212 @print{} 'count_calls' has been called 3 times | |
213 @end group | |
214 @end example | |
215 | |
216 As the example shows, a variable may be declared persistent using a | |
217 @code{persistent} declaration statement. The following statements are | |
218 all persistent declarations. | |
4686 | 219 |
220 @example | |
221 @group | |
222 persistent a | |
223 persistent a b | |
224 persistent c = 2 | |
225 persistent d = 3 e f = 5 | |
226 @end group | |
227 @end example | |
228 | |
229 The behavior of persistent variables is equivalent to the behavior of | |
8481
00df69d7e698
[docs] capitalize Octave consistently
Brian Gough <bjg@gnu.org>
parents:
8347
diff
changeset
|
230 static variables in C. The command @code{static} in Octave is also |
6899 | 231 recognized and is equivalent to @code{persistent}. |
232 | |
233 Like global variables, a persistent variable may only be initialized once. | |
6896 | 234 For example, after executing the following code |
4686 | 235 |
236 @example | |
237 @group | |
238 persistent pvar = 1 | |
239 persistent pvar = 2 | |
240 @end group | |
241 @end example | |
242 | |
243 @noindent | |
6896 | 244 the value of the persistent variable @code{pvar} is 1, not 2. |
4686 | 245 |
6899 | 246 If a persistent variable is declared but not initialized to a specific |
247 value, it will contain an empty matrix. So, it is also possible to | |
248 initialize a persistent variable by checking whether it is empty, as the | |
249 following example illustrates. | |
250 | |
251 @example | |
252 @group | |
253 function count_calls () | |
254 persistent calls; | |
255 if (isempty (calls)) | |
256 calls = 0; | |
257 endif | |
7031 | 258 printf ("'count_calls' has been called %d times\n", |
259 ++calls); | |
6899 | 260 endfunction |
261 @end group | |
262 @end example | |
263 | |
264 @noindent | |
265 This implementation behaves in exactly the same way as the previous | |
266 implementation of @code{count_calls}. | |
267 | |
268 The value of a persistent variable is kept in memory until it is | |
269 explicitly cleared. Assuming that the implementation of @code{count_calls} | |
270 is saved on disc, we get the following behaviour. | |
271 | |
272 @example | |
273 @group | |
274 for i = 1:2 | |
275 count_calls (); | |
276 endfor | |
277 @print{} 'count_calls' has been called 1 times | |
278 @print{} 'count_calls' has been called 2 times | |
279 | |
280 clear | |
281 for i = 1:2 | |
282 count_calls(); | |
283 endfor | |
284 @print{} 'count_calls' has been called 3 times | |
285 @print{} 'count_calls' has been called 4 times | |
286 | |
287 clear all | |
288 for i = 1:2 | |
289 count_calls(); | |
290 endfor | |
291 @print{} 'count_calls' has been called 1 times | |
292 @print{} 'count_calls' has been called 2 times | |
293 | |
294 clear count_calls | |
295 for i = 1:2 | |
296 count_calls(); | |
297 endfor | |
298 @print{} 'count_calls' has been called 1 times | |
299 @print{} 'count_calls' has been called 2 times | |
300 @end group | |
301 @end example | |
302 | |
303 @noindent | |
304 That is, the persistent variable is only removed from memory when the | |
305 function containing the variable is removed. Note that if the function | |
306 definition is typed directly into the Octave prompt, the persistent | |
307 variable will be cleared by a simple @code{clear} command as the entire | |
308 function definition will be removed from memory. If you do not want | |
309 a persistent variable to be removed from memory even if the function is | |
310 cleared, you should use the @code{mlock} function as described in | |
311 @xref{Function Locking}. | |
312 | |
4167 | 313 @node Status of Variables |
3294 | 314 @section Status of Variables |
315 | |
6623 | 316 When creating simple one-shot programs it can be very convenient to |
317 see which variables are available at the prompt. The function @code{who} | |
318 and its siblings @code{whos} and @code{whos_line_format} will show | |
319 different information about what is in memory, as the following shows. | |
320 | |
321 @example | |
322 str = "A random string"; | |
323 who -variables | |
324 @print{} *** local user variables: | |
325 @print{} | |
326 @print{} __nargin__ str | |
327 @end example | |
3294 | 328 |
3361 | 329 @DOCSTRING(who) |
3294 | 330 |
4913 | 331 @DOCSTRING(whos) |
332 | |
333 @DOCSTRING(whos_line_format) | |
334 | |
6623 | 335 Instead of displaying which variables are in memory, it is possible |
336 to determine if a given variable is available. That way it is possible | |
337 to alter the behaviour of a program depending on the existence of a | |
338 variable. The following example illustrates this. | |
339 | |
340 @example | |
341 if (! exist ("meaning", "var")) | |
342 disp ("The program has no 'meaning'"); | |
343 endif | |
344 @end example | |
345 | |
3361 | 346 @DOCSTRING(exist) |
3294 | 347 |
6623 | 348 Usually Octave will manage the memory, but sometimes it can be practical |
349 to remove variables from memory manually. This is usually needed when | |
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, | |
360 it can be necessary to remove it manually from memory. The @code{clear} | |
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 |
6623 | 366 file system can also be acquired from within Octave. This is usually |
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 |