3294
|
1 @c Copyright (C) 1996, 1997 John W. Eaton |
|
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 |
|
99 @samp{clear a} command does not change the above behavior, but |
|
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 |
3361
|
206 @DOCSTRING(clear) |
3294
|
207 |
3361
|
208 @DOCSTRING(who) |
3294
|
209 |
4913
|
210 @DOCSTRING(whos) |
|
211 |
|
212 @DOCSTRING(whos_line_format) |
|
213 |
3361
|
214 @DOCSTRING(exist) |
3294
|
215 |
3361
|
216 @DOCSTRING(document) |
3294
|
217 |
3361
|
218 @DOCSTRING(type) |
3294
|
219 |
3361
|
220 @DOCSTRING(which) |
3294
|
221 |
4167
|
222 @node Summary of Built-in Variables |
3294
|
223 @section Summary of Built-in Variables |
|
224 |
|
225 Here is a summary of all of Octave's built-in variables along with |
|
226 cross references to additional information and their default values. In |
|
227 the following table @var{octave-home} stands for the root directory |
|
228 where all of Octave is installed (the default is @file{@value{OCTAVEHOME}}, |
|
229 @var{version} stands for the Octave version number (for example, |
|
230 @value{VERSION}) and @var{arch} stands for the type of system for which |
5942
|
231 Octave was compiled (for example, @code{x86_64-unknown-linux-gnu}). |
3294
|
232 |
|
233 @vtable @code |
|
234 @item EDITOR |
|
235 @xref{Commands For History}. |
|
236 |
|
237 Default value: @code{"emacs"}. |
|
238 |
|
239 @item EXEC_PATH |
|
240 @xref{Controlling Subprocesses}. |
|
241 |
|
242 Default value: @code{":$PATH"}. |
|
243 |
|
244 @item OCTAVE_HOME |
|
245 |
|
246 Default value: @code{"@value{OCTAVEHOME}"}. |
|
247 |
|
248 @item PAGER |
|
249 @xref{Input and Output}. |
|
250 |
|
251 Default value: @code{"less", or "more"}. |
|
252 |
|
253 @item PS1 |
|
254 @xref{Customizing the Prompt}. |
|
255 |
|
256 Default value: @code{"\s:\#> "}. |
|
257 |
|
258 @item PS2 |
|
259 @xref{Customizing the Prompt}. |
|
260 |
|
261 Default value: @code{"> "}. |
|
262 |
|
263 @item PS4 |
|
264 @xref{Customizing the Prompt}. |
|
265 |
|
266 Default value: @code{"+ "}. |
|
267 |
|
268 @item beep_on_error |
|
269 @xref{Error Handling}. |
|
270 |
|
271 Default value: 0. |
|
272 |
|
273 @item completion_append_char |
|
274 @xref{Commands For Completion}. |
|
275 |
|
276 Default value: @code{" "}. |
|
277 |
5287
|
278 @item default_save_options |
3294
|
279 @xref{Simple File I/O}. |
|
280 |
|
281 Default value: @code{"ascii"}. |
|
282 |
|
283 @item crash_dumps_octave_core |
|
284 @xref{Simple File I/O}. |
|
285 |
|
286 Default value: 1. |
|
287 |
|
288 @item fixed_point_format |
|
289 @xref{Matrices}. |
|
290 |
|
291 Default value: 0. |
|
292 |
|
293 @item gnuplot_binary |
|
294 @xref{Three-Dimensional Plotting}. |
|
295 |
|
296 Default value: @code{"gnuplot"}. |
|
297 |
|
298 @item history_file |
|
299 @xref{Commands For History}. |
|
300 |
|
301 Default value: @code{"~/.octave_hist"}. |
|
302 |
|
303 @item history_size |
|
304 @xref{Commands For History}. |
|
305 |
|
306 Default value: 1024. |
|
307 |
|
308 @item ignore_function_time_stamp |
|
309 @xref{Function Files}. |
|
310 |
|
311 Default value: @code{"system"}. |
|
312 |
|
313 @item max_recursion_depth |
|
314 @xref{Recursion}. |
|
315 |
|
316 Default value: 256. |
|
317 |
|
318 @item output_max_field_width |
|
319 @xref{Matrices}. |
|
320 |
|
321 Default value: 10. |
|
322 |
|
323 @item output_precision |
|
324 @xref{Matrices}. |
|
325 |
|
326 Default value: 5. |
|
327 |
|
328 @item page_screen_output |
|
329 @xref{Input and Output}. |
|
330 |
|
331 Default value: 1. |
|
332 |
|
333 @item print_answer_id_name |
|
334 @xref{Terminal Output}. |
|
335 |
|
336 Default value: 1. |
|
337 |
|
338 @item print_empty_dimensions |
|
339 @xref{Empty Matrices}. |
|
340 |
|
341 Default value: 1. |
|
342 |
|
343 @item return_last_computed_value |
|
344 @xref{Returning From a Function}. |
|
345 |
|
346 Default value: 0. |
|
347 |
|
348 @item save_precision |
|
349 @xref{Simple File I/O}. |
|
350 |
|
351 Default value: 17. |
|
352 |
|
353 @item saving_history |
|
354 @xref{Commands For History}. |
|
355 |
|
356 Default value: 1. |
|
357 |
4449
|
358 @item sighup_dumps_octave_core |
|
359 @xref{Simple File I/O}. |
|
360 |
|
361 Default value: 1. |
|
362 |
|
363 @item sigterm_dumps_octave_core |
|
364 @xref{Simple File I/O}. |
|
365 |
|
366 Default value: 1. |
|
367 |
3294
|
368 @item silent_functions |
|
369 @xref{Defining Functions}. |
|
370 |
|
371 Default value: 0. |
|
372 |
|
373 @item split_long_rows |
|
374 @xref{Matrices}. |
|
375 |
|
376 Default value: 1. |
|
377 |
|
378 @item struct_levels_to_print |
|
379 @xref{Data Structures}. |
|
380 |
|
381 Default value: 2. |
|
382 |
|
383 @item suppress_verbose_help_message |
|
384 @xref{Getting Help}. |
|
385 |
|
386 Default value: 1. |
|
387 @end vtable |
|
388 |
|
389 |
4167
|
390 @node Defaults from the Environment |
3294
|
391 @section Defaults from the Environment |
|
392 |
|
393 Octave uses the values of the following environment variables to set the |
6477
|
394 default values for the corresponding built-in or internal variables. |
|
395 In addition, the values from the environment may be overridden by |
|
396 command-line arguments. @xref{Command Line Options}. |
3294
|
397 |
|
398 @vtable @code |
|
399 @item EDITOR |
|
400 @xref{Commands For History}. |
|
401 |
|
402 Built-in variable: @code{EDITOR}. |
|
403 |
|
404 @item OCTAVE_EXEC_PATH |
|
405 @xref{Controlling Subprocesses}. |
|
406 |
|
407 Built-in variable: @code{EXEC_PATH}. |
|
408 Command-line argument: @code{--exec-path}. |
|
409 |
|
410 @item OCTAVE_PATH |
|
411 @xref{Function Files}. |
|
412 |
6477
|
413 Internal variable changed by function @code{path}. |
3294
|
414 Command-line argument: @code{--path}. |
|
415 |
|
416 @item OCTAVE_INFO_FILE |
|
417 @xref{Getting Help}. |
|
418 |
6477
|
419 Internal variable changed by function @code{info_file}. |
3294
|
420 Command-line argument: @code{--info-file}. |
|
421 |
|
422 @item OCTAVE_INFO_PROGRAM |
|
423 @xref{Getting Help}. |
|
424 |
6477
|
425 Internal variable changed by function @code{info_program}. |
3294
|
426 Command-line argument: @code{--info-program}. |
|
427 |
|
428 @item OCTAVE_HISTSIZE |
|
429 @xref{Commands For History}. |
|
430 |
|
431 Built-in variable: @code{history_size}. |
|
432 |
|
433 @item OCTAVE_HISTFILE |
|
434 @xref{Commands For History}. |
|
435 |
|
436 Built-in variable: @code{history_file}. |
|
437 @end vtable |