Mercurial > octave-antonio
annotate doc/interpreter/var.txi @ 8817:03b7f618ab3d
include docstrings for new functions in the manual
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 19 Feb 2009 15:39:19 -0500 |
parents | 38ee7ce3bc7d |
children | eb63fbe60fab |
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 @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 | |
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 | |
8481
00df69d7e698
[docs] capitalize Octave consistently
Brian Gough <bjg@gnu.org>
parents:
8347
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} | |
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 | |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
8286
diff
changeset
|
363 Information about a function or variable such as its location in the |
6623 | 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(type) |
3294 | 368 |
3361 | 369 @DOCSTRING(which) |
3294 | 370 |
8817
03b7f618ab3d
include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents:
8571
diff
changeset
|
371 @DOCSTRING(what) |