6778
|
1 @c Copyright (C) 1996, 1997, 2007 John W. Eaton |
3294
|
2 @c This is part of the Octave manual. |
|
3 @c For copying conditions, see the file gpl.texi. |
|
4 |
4167
|
5 @node Variables |
3294
|
6 @chapter Variables |
|
7 @cindex variables, user-defined |
|
8 @cindex user-defined variables |
|
9 |
|
10 Variables let you give names to values and refer to them later. You have |
|
11 already seen variables in many of the examples. The name of a variable |
|
12 must be a sequence of letters, digits and underscores, but it may not begin |
|
13 with a digit. Octave does not enforce a limit on the length of variable |
|
14 names, but it is seldom useful to have variables with names longer than |
|
15 about 30 characters. The following are all valid variable names |
|
16 |
|
17 @cindex job hunting |
|
18 @cindex getting a good job |
|
19 @cindex flying high and fast |
|
20 @example |
|
21 @group |
|
22 x |
|
23 x15 |
|
24 __foo_bar_baz__ |
|
25 fucnrdthsucngtagdjb |
|
26 @end group |
|
27 @end example |
|
28 |
|
29 @noindent |
|
30 However, names like @code{__foo_bar_baz__} that begin and end with two |
|
31 underscores are understood to be reserved for internal use by Octave. |
|
32 You should not use them in code you write, except to access Octave's |
|
33 documented internal variables and built-in symbolic constants. |
|
34 |
|
35 Case is significant in variable names. The symbols @code{a} and |
|
36 @code{A} are distinct variables. |
|
37 |
|
38 A variable name is a valid expression by itself. It represents the |
|
39 variable's current value. Variables are given new values with |
|
40 @dfn{assignment operators} and @dfn{increment operators}. |
|
41 @xref{Assignment Ops, ,Assignment Expressions}. |
|
42 |
|
43 A number of variables have special built-in meanings. For example, |
|
44 @code{ans} holds the current working directory, and @code{pi} names the |
|
45 ratio of the circumference of a circle to its diameter. @xref{Summary of |
|
46 Built-in Variables}, for a list of all the predefined variables. Some |
|
47 of these built-in symbols are constants and may not be changed. Others |
|
48 can be used and assigned just like all other variables, but their values |
|
49 are also used or changed automatically by Octave. |
|
50 |
|
51 Variables in Octave do not have fixed types, so it is possible to first |
|
52 store a numeric value in a variable and then to later use the same name |
|
53 to hold a string value in the same program. Variables may not be used |
|
54 before they have been given a value. Doing so results in an error. |
|
55 |
6550
|
56 @DOCSTRING(isvarname) |
|
57 |
3294
|
58 @menu |
|
59 * Global Variables:: |
4686
|
60 * Persistent Variables:: |
3294
|
61 * Status of Variables:: |
|
62 * Summary of Built-in Variables:: |
|
63 * Defaults from the Environment:: |
|
64 @end menu |
|
65 |
4167
|
66 @node Global Variables |
3294
|
67 @section Global Variables |
|
68 @cindex global variables |
|
69 @cindex @code{global} statement |
|
70 @cindex variables, global |
|
71 |
|
72 A variable that has been declared @dfn{global} may be accessed from |
|
73 within a function body without having to pass it as a formal parameter. |
|
74 |
|
75 A variable may be declared global using a @code{global} declaration |
|
76 statement. The following statements are all global declarations. |
|
77 |
|
78 @example |
|
79 @group |
|
80 global a |
4504
|
81 global a b |
|
82 global c = 2 |
|
83 global d = 3 e f = 5 |
3294
|
84 @end group |
|
85 @end example |
|
86 |
4504
|
87 A global variable may only be initialized once in a @code{global} |
|
88 statement. For example, after executing the following code |
|
89 |
|
90 @example |
|
91 @group |
|
92 global gvar = 1 |
|
93 global gvar = 2 |
|
94 @end group |
|
95 @end example |
|
96 |
|
97 @noindent |
6077
|
98 the value of the global variable @code{gvar} is 1, not 2. Issuing a |
6623
|
99 @samp{clear gvar} command does not change the above behavior, but |
6077
|
100 @samp{clear all} does. |
4504
|
101 |
3294
|
102 It is necessary declare a variable as global within a function body in |
|
103 order to access it. For example, |
|
104 |
|
105 @example |
|
106 @group |
|
107 global x |
|
108 function f () |
|
109 x = 1; |
|
110 endfunction |
|
111 f () |
|
112 @end group |
|
113 @end example |
|
114 |
|
115 @noindent |
|
116 does @emph{not} set the value of the global variable @code{x} to 1. In |
|
117 order to change the value of the global variable @code{x}, you must also |
|
118 declare it to be global within the function body, like this |
|
119 |
|
120 @example |
|
121 @group |
|
122 function f () |
|
123 global x; |
|
124 x = 1; |
|
125 endfunction |
|
126 @end group |
|
127 @end example |
|
128 |
|
129 Passing a global variable in a function parameter list will |
|
130 make a local copy and not modify the global value. For example, given |
|
131 the function |
|
132 |
|
133 @example |
|
134 @group |
|
135 function f (x) |
|
136 x = 0 |
|
137 endfunction |
|
138 @end group |
|
139 @end example |
|
140 |
|
141 @noindent |
|
142 and the definition of @code{x} as a global variable at the top level, |
|
143 |
|
144 @example |
|
145 global x = 13 |
|
146 @end example |
|
147 |
|
148 @noindent |
|
149 the expression |
|
150 |
|
151 @example |
|
152 f (x) |
|
153 @end example |
|
154 |
|
155 @noindent |
|
156 will display the value of @code{x} from inside the function as 0, |
|
157 but the value of @code{x} at the top level remains unchanged, because |
|
158 the function works with a @emph{copy} of its argument. |
|
159 |
4029
|
160 @DOCSTRING(isglobal) |
3294
|
161 |
4686
|
162 @node Persistent Variables |
|
163 @section Persistent Variables |
|
164 @cindex persistent variables |
|
165 @cindex @code{persistent} statement |
|
166 @cindex variables, persistent |
|
167 |
|
168 A variable that has been declared @dfn{persistent} within a function |
|
169 will retain its contents in memory between subsequent calls to the |
|
170 same function. The difference between persistent variables and global |
|
171 variables is that persistent variables are local in scope to a |
|
172 particular function and are not visible elsewhere. |
|
173 |
|
174 A variable may be declared persistent using a @code{persistent} |
|
175 declaration statement. The following statements are all persistent |
|
176 declarations. |
|
177 |
|
178 @example |
|
179 @group |
|
180 persistent a |
|
181 persistent a b |
|
182 persistent c = 2 |
|
183 persistent d = 3 e f = 5 |
|
184 @end group |
|
185 @end example |
|
186 |
|
187 The behavior of persistent variables is equivalent to the behavior of |
|
188 static variables in C. The command @code{static} in octave is also |
|
189 recognized and is equivalent to @code{persistent}. Unlike global |
|
190 variables, every initialization statement will re-initialize the |
|
191 variable. For example, after executing the following code |
|
192 |
|
193 @example |
|
194 @group |
|
195 persistent pvar = 1 |
|
196 persistent pvar = 2 |
|
197 @end group |
|
198 @end example |
|
199 |
|
200 @noindent |
|
201 the value of the persistent variable @code{pvar} is 2. |
|
202 |
4167
|
203 @node Status of Variables |
3294
|
204 @section Status of Variables |
|
205 |
6623
|
206 When creating simple one-shot programs it can be very convenient to |
|
207 see which variables are available at the prompt. The function @code{who} |
|
208 and its siblings @code{whos} and @code{whos_line_format} will show |
|
209 different information about what is in memory, as the following shows. |
|
210 |
|
211 @example |
|
212 str = "A random string"; |
|
213 who -variables |
|
214 @print{} *** local user variables: |
|
215 @print{} |
|
216 @print{} __nargin__ str |
|
217 @end example |
3294
|
218 |
3361
|
219 @DOCSTRING(who) |
3294
|
220 |
4913
|
221 @DOCSTRING(whos) |
|
222 |
|
223 @DOCSTRING(whos_line_format) |
|
224 |
6623
|
225 Instead of displaying which variables are in memory, it is possible |
|
226 to determine if a given variable is available. That way it is possible |
|
227 to alter the behaviour of a program depending on the existence of a |
|
228 variable. The following example illustrates this. |
|
229 |
|
230 @example |
|
231 if (! exist ("meaning", "var")) |
|
232 disp ("The program has no 'meaning'"); |
|
233 endif |
|
234 @end example |
|
235 |
3361
|
236 @DOCSTRING(exist) |
3294
|
237 |
6623
|
238 Usually Octave will manage the memory, but sometimes it can be practical |
|
239 to remove variables from memory manually. This is usually needed when |
|
240 working with large variables that fill a substantial part of the memory. |
|
241 On a computer that uses the IEEE floating point format, the following |
|
242 program allocates a matrix that requires around 128 MB memory. |
|
243 |
|
244 @example |
|
245 large_matrix = zeros (4000, 4000); |
|
246 @end example |
|
247 |
|
248 @noindent |
|
249 Since having this variable in memory might slow down other computations, |
|
250 it can be necessary to remove it manually from memory. The @code{clear} |
|
251 function allows this. |
|
252 |
|
253 @DOCSTRING(clear) |
|
254 |
|
255 Information about a function or variable such as it's location in the |
|
256 file system can also be acquired from within Octave. This is usually |
|
257 only useful during development of programs, and not within a program. |
|
258 |
3361
|
259 @DOCSTRING(document) |
3294
|
260 |
3361
|
261 @DOCSTRING(type) |
3294
|
262 |
3361
|
263 @DOCSTRING(which) |
3294
|
264 |
4167
|
265 @node Summary of Built-in Variables |
3294
|
266 @section Summary of Built-in Variables |
|
267 |
|
268 Here is a summary of all of Octave's built-in variables along with |
|
269 cross references to additional information and their default values. In |
|
270 the following table @var{octave-home} stands for the root directory |
|
271 where all of Octave is installed (the default is @file{@value{OCTAVEHOME}}, |
|
272 @var{version} stands for the Octave version number (for example, |
|
273 @value{VERSION}) and @var{arch} stands for the type of system for which |
5942
|
274 Octave was compiled (for example, @code{x86_64-unknown-linux-gnu}). |
3294
|
275 |
|
276 @vtable @code |
|
277 @item EDITOR |
|
278 @xref{Commands For History}. |
|
279 |
|
280 Default value: @code{"emacs"}. |
|
281 |
|
282 @item EXEC_PATH |
|
283 @xref{Controlling Subprocesses}. |
|
284 |
|
285 Default value: @code{":$PATH"}. |
|
286 |
|
287 @item OCTAVE_HOME |
|
288 |
|
289 Default value: @code{"@value{OCTAVEHOME}"}. |
|
290 |
|
291 @item PAGER |
|
292 @xref{Input and Output}. |
|
293 |
|
294 Default value: @code{"less", or "more"}. |
|
295 |
|
296 @item PS1 |
|
297 @xref{Customizing the Prompt}. |
|
298 |
|
299 Default value: @code{"\s:\#> "}. |
|
300 |
|
301 @item PS2 |
|
302 @xref{Customizing the Prompt}. |
|
303 |
|
304 Default value: @code{"> "}. |
|
305 |
|
306 @item PS4 |
|
307 @xref{Customizing the Prompt}. |
|
308 |
|
309 Default value: @code{"+ "}. |
|
310 |
|
311 @item beep_on_error |
6667
|
312 @xref{Errors and Warnings}. |
3294
|
313 |
|
314 Default value: 0. |
|
315 |
|
316 @item completion_append_char |
|
317 @xref{Commands For Completion}. |
|
318 |
|
319 Default value: @code{" "}. |
|
320 |
5287
|
321 @item default_save_options |
3294
|
322 @xref{Simple File I/O}. |
|
323 |
|
324 Default value: @code{"ascii"}. |
|
325 |
|
326 @item crash_dumps_octave_core |
|
327 @xref{Simple File I/O}. |
|
328 |
|
329 Default value: 1. |
|
330 |
|
331 @item fixed_point_format |
|
332 @xref{Matrices}. |
|
333 |
|
334 Default value: 0. |
|
335 |
|
336 @item gnuplot_binary |
|
337 @xref{Three-Dimensional Plotting}. |
|
338 |
|
339 Default value: @code{"gnuplot"}. |
|
340 |
|
341 @item history_file |
|
342 @xref{Commands For History}. |
|
343 |
|
344 Default value: @code{"~/.octave_hist"}. |
|
345 |
|
346 @item history_size |
|
347 @xref{Commands For History}. |
|
348 |
|
349 Default value: 1024. |
|
350 |
|
351 @item ignore_function_time_stamp |
|
352 @xref{Function Files}. |
|
353 |
|
354 Default value: @code{"system"}. |
|
355 |
|
356 @item max_recursion_depth |
|
357 @xref{Recursion}. |
|
358 |
|
359 Default value: 256. |
|
360 |
|
361 @item output_max_field_width |
|
362 @xref{Matrices}. |
|
363 |
|
364 Default value: 10. |
|
365 |
|
366 @item output_precision |
|
367 @xref{Matrices}. |
|
368 |
|
369 Default value: 5. |
|
370 |
|
371 @item page_screen_output |
|
372 @xref{Input and Output}. |
|
373 |
|
374 Default value: 1. |
|
375 |
|
376 @item print_answer_id_name |
|
377 @xref{Terminal Output}. |
|
378 |
|
379 Default value: 1. |
|
380 |
|
381 @item print_empty_dimensions |
|
382 @xref{Empty Matrices}. |
|
383 |
|
384 Default value: 1. |
|
385 |
|
386 @item return_last_computed_value |
|
387 @xref{Returning From a Function}. |
|
388 |
|
389 Default value: 0. |
|
390 |
|
391 @item save_precision |
|
392 @xref{Simple File I/O}. |
|
393 |
|
394 Default value: 17. |
|
395 |
|
396 @item saving_history |
|
397 @xref{Commands For History}. |
|
398 |
|
399 Default value: 1. |
|
400 |
4449
|
401 @item sighup_dumps_octave_core |
|
402 @xref{Simple File I/O}. |
|
403 |
|
404 Default value: 1. |
|
405 |
|
406 @item sigterm_dumps_octave_core |
|
407 @xref{Simple File I/O}. |
|
408 |
|
409 Default value: 1. |
|
410 |
3294
|
411 @item silent_functions |
|
412 @xref{Defining Functions}. |
|
413 |
|
414 Default value: 0. |
|
415 |
|
416 @item split_long_rows |
|
417 @xref{Matrices}. |
|
418 |
|
419 Default value: 1. |
|
420 |
|
421 @item struct_levels_to_print |
|
422 @xref{Data Structures}. |
|
423 |
|
424 Default value: 2. |
|
425 |
|
426 @item suppress_verbose_help_message |
|
427 @xref{Getting Help}. |
|
428 |
|
429 Default value: 1. |
|
430 @end vtable |
|
431 |
|
432 |
4167
|
433 @node Defaults from the Environment |
3294
|
434 @section Defaults from the Environment |
|
435 |
|
436 Octave uses the values of the following environment variables to set the |
6477
|
437 default values for the corresponding built-in or internal variables. |
|
438 In addition, the values from the environment may be overridden by |
|
439 command-line arguments. @xref{Command Line Options}. |
3294
|
440 |
|
441 @vtable @code |
|
442 @item EDITOR |
|
443 @xref{Commands For History}. |
|
444 |
|
445 Built-in variable: @code{EDITOR}. |
|
446 |
|
447 @item OCTAVE_EXEC_PATH |
|
448 @xref{Controlling Subprocesses}. |
|
449 |
|
450 Built-in variable: @code{EXEC_PATH}. |
|
451 Command-line argument: @code{--exec-path}. |
|
452 |
|
453 @item OCTAVE_PATH |
|
454 @xref{Function Files}. |
|
455 |
6477
|
456 Internal variable changed by function @code{path}. |
3294
|
457 Command-line argument: @code{--path}. |
|
458 |
|
459 @item OCTAVE_INFO_FILE |
|
460 @xref{Getting Help}. |
|
461 |
6477
|
462 Internal variable changed by function @code{info_file}. |
3294
|
463 Command-line argument: @code{--info-file}. |
|
464 |
|
465 @item OCTAVE_INFO_PROGRAM |
|
466 @xref{Getting Help}. |
|
467 |
6477
|
468 Internal variable changed by function @code{info_program}. |
3294
|
469 Command-line argument: @code{--info-program}. |
|
470 |
|
471 @item OCTAVE_HISTSIZE |
|
472 @xref{Commands For History}. |
|
473 |
|
474 Built-in variable: @code{history_size}. |
|
475 |
|
476 @item OCTAVE_HISTFILE |
|
477 @xref{Commands For History}. |
|
478 |
|
479 Built-in variable: @code{history_file}. |
|
480 @end vtable |