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 Getting Started |
3294
|
6 @chapter Getting Started |
|
7 |
|
8 This chapter explains some of Octave's basic features, including how to |
|
9 start an Octave session, get help at the command prompt, edit the |
|
10 command line, and write Octave programs that can be executed as commands |
|
11 from your shell. |
|
12 |
|
13 @menu |
|
14 * Invoking Octave:: |
|
15 * Quitting Octave:: |
|
16 * Getting Help:: |
|
17 * Command Line Editing:: |
|
18 * Errors:: |
|
19 * Executable Octave Programs:: |
|
20 * Comments:: |
|
21 @end menu |
|
22 |
4167
|
23 @node Invoking Octave |
3294
|
24 @section Invoking Octave |
|
25 |
|
26 Normally, Octave is used interactively by running the program |
|
27 @samp{octave} without any arguments. Once started, Octave reads |
|
28 commands from the terminal until you tell it to exit. |
|
29 |
|
30 You can also specify the name of a file on the command line, and Octave |
|
31 will read and execute the commands from the named file and then exit |
|
32 when it is finished. |
|
33 |
|
34 You can further control how Octave starts by using the command-line |
|
35 options described in the next section, and Octave itself can remind you |
|
36 of the options available. Type @samp{octave --help} to display all |
|
37 available options and briefly describe their use (@samp{octave -h} is a |
|
38 shorter equivalent). |
|
39 |
|
40 @menu |
|
41 * Command Line Options:: |
|
42 * Startup Files:: |
|
43 @end menu |
|
44 |
4167
|
45 @node Command Line Options |
3294
|
46 @subsection Command Line Options |
|
47 @cindex Octave command options |
|
48 @cindex command options |
|
49 @cindex options, Octave command |
|
50 |
|
51 Here is a complete list of all the command line options that Octave |
|
52 accepts. |
|
53 |
|
54 @table @code |
|
55 @item --debug |
|
56 @itemx -d |
|
57 @cindex @code{--debug} |
|
58 @cindex @code{-d} |
|
59 Enter parser debugging mode. Using this option will cause Octave's |
|
60 parser to print a lot of information about the commands it reads, and is |
|
61 probably only useful if you are actually trying to debug the parser. |
|
62 |
|
63 @item --echo-commands |
|
64 @itemx -x |
|
65 @cindex @code{--echo-commands} |
|
66 @cindex @code{-x} |
|
67 Echo commands as they are executed. |
|
68 |
|
69 @item --exec-path @var{path} |
|
70 @cindex @code{--exec-path @var{path}} |
|
71 Specify the path to search for programs to run. The value of @var{path} |
|
72 specified on the command line will override any value of |
|
73 @code{OCTAVE_EXEC_PATH} found in the environment, but not any commands |
|
74 in the system or user startup files that set the built-in variable |
|
75 @code{EXEC_PATH}. |
|
76 |
|
77 @item --help |
|
78 @itemx -h |
|
79 @itemx -? |
|
80 @cindex @code{--help} |
|
81 @cindex @code{-h} |
|
82 @cindex @code{-?} |
|
83 Print short help message and exit. |
|
84 |
|
85 @item --info-file @var{filename} |
|
86 @cindex @code{--info-file @var{filename}} |
|
87 Specify the name of the info file to use. The value of @var{filename} |
|
88 specified on the command line will override any value of |
|
89 @code{OCTAVE_INFO_FILE} found in the environment, but not any commands |
|
90 in the system or user startup files that set the built-in variable |
|
91 @code{INFO_FILE}. |
|
92 |
|
93 @item --info-program @var{program} |
|
94 @cindex @code{--info-program @var{program}} |
|
95 Specify the name of the info program to use. The value of @var{program} |
|
96 specified on the command line will override any value of |
|
97 @code{OCTAVE_INFO_PROGRAM} found in the environment, but not any |
|
98 commands in the system or user startup files that set the built-in |
|
99 variable @code{INFO_PROGRAM}. |
|
100 |
|
101 @item --interactive |
|
102 @itemx -i |
|
103 @cindex @code{--interactive} |
|
104 @cindex @code{-i} |
|
105 Force interactive behavior. This can be useful for running Octave via a |
|
106 remote shell command or inside an Emacs shell buffer. For another way |
|
107 to run Octave within Emacs, see @ref{Emacs}. |
|
108 |
3428
|
109 @item --no-history |
|
110 @itemx -H |
|
111 Disable command-line history. |
|
112 |
3294
|
113 @item --no-init-file |
|
114 @cindex @code{--no-init-file} |
|
115 Don't read the @file{~/.octaverc} or @file{.octaverc} files. |
|
116 |
|
117 @item --no-line-editing |
|
118 @cindex @code{--no-line-editing} |
|
119 Disable command-line editing. |
|
120 |
|
121 @item --no-site-file |
|
122 @cindex @code{--no-site-file} |
|
123 Don't read the site-wide @file{octaverc} file. |
|
124 |
|
125 @item --norc |
|
126 @itemx -f |
|
127 @cindex @code{--norc} |
|
128 @cindex @code{-f} |
|
129 Don't read any of the system or user initialization files at startup. |
|
130 This is equivalent to using both of the options @code{--no-init-file} |
|
131 and @code{--no-site-file}. |
|
132 |
|
133 @item --path @var{path} |
|
134 @itemx -p @var{path} |
|
135 @cindex @code{--path @var{path}} |
|
136 @cindex @code{-p @var{path}} |
|
137 Specify the path to search for function files. The value of @var{path} |
|
138 specified on the command line will override any value of |
|
139 @code{OCTAVE_PATH} found in the environment, but not any commands in the |
|
140 system or user startup files that set the built-in variable @code{LOADPATH}. |
|
141 |
|
142 @item --silent |
|
143 @itemx --quiet |
|
144 @itemx -q |
|
145 @cindex @code{--silent} |
|
146 @cindex @code{--quiet} |
|
147 @cindex @code{-q} |
|
148 Don't print the usual greeting and version message at startup. |
|
149 |
|
150 @item --traditional |
|
151 @itemx --braindead |
|
152 @cindex @code{--traditional} |
|
153 @cindex @code{--braindead} |
|
154 Set initial values for user-preference variables to the following |
|
155 values for compatibility with @sc{Matlab}. |
|
156 |
|
157 @example |
4455
|
158 @group |
|
159 PS1 = ">> " |
|
160 PS2 = "" |
|
161 beep_on_error = true |
|
162 crash_dumps_octave_core = false |
|
163 default_eval_print_flag = false |
|
164 default_global_variable_value = [] |
|
165 default_save_format = "mat-binary" |
|
166 define_all_return_values = true |
|
167 fixed_point_format = true |
|
168 initialize_global_variables = true |
|
169 page_screen_output = false |
|
170 print_empty_dimensions = false |
|
171 treat_neg_dim_as_zero = true |
|
172 warn_function_name_clash = false |
|
173 whitespace_in_literal_matrix = "traditional" |
|
174 @end group |
3294
|
175 @end example |
|
176 |
|
177 @item --verbose |
|
178 @itemx -V |
|
179 @cindex @code{--verbose} |
|
180 @cindex @code{-V} |
|
181 Turn on verbose output. |
|
182 |
|
183 @item --version |
|
184 @itemx -v |
|
185 @cindex @code{--version} |
|
186 @cindex @code{-v} |
|
187 Print the program version number and exit. |
|
188 |
|
189 @item @var{file} |
|
190 Execute commands from @var{file}. |
|
191 @end table |
|
192 |
|
193 Octave also includes several built-in variables that contain information |
|
194 about the command line, including the number of arguments and all of the |
|
195 options. |
|
196 |
3332
|
197 @DOCSTRING(argv) |
3294
|
198 |
3332
|
199 @DOCSTRING(program_invocation_name) |
3294
|
200 |
|
201 Here is an example of using these variables to reproduce Octave's |
|
202 command line. |
|
203 |
|
204 @example |
|
205 printf ("%s", program_name); |
|
206 for i = 1:nargin |
3803
|
207 printf (" %s", nth (argv, i)); |
3294
|
208 endfor |
|
209 printf ("\n"); |
|
210 @end example |
|
211 |
|
212 @noindent |
3402
|
213 @xref{Index Expressions}, for an explanation of how to properly index |
|
214 arrays of strings and substrings in Octave, and @xref{Defining Functions}, |
3294
|
215 for information about the variable @code{nargin}. |
|
216 |
4167
|
217 @node Startup Files |
3294
|
218 @subsection Startup Files |
|
219 @cindex initialization |
|
220 @cindex startup |
|
221 |
|
222 When Octave starts, it looks for commands to execute from the following |
|
223 files: |
|
224 |
|
225 @cindex startup files |
|
226 |
|
227 @table @code |
|
228 @item @var{octave-home}/share/octave/site/m/startup/octaverc |
|
229 Where @var{octave-home} is the directory in which all of Octave is |
|
230 installed (the default is @file{@value{OCTAVEHOME}}). This file is |
|
231 provided so that changes to the default Octave environment can be made |
|
232 globally for all users at your site for all versions of Octave you have |
|
233 installed. Some care should be taken when making changes to this file, |
|
234 since all users of Octave at your site will be affected. |
|
235 |
|
236 @item @var{octave-home}/share/octave/@var{version}/m/startup/octaverc |
|
237 Where @var{octave-home} is the directory in which all of Octave is |
|
238 installed (the default is @file{@value{OCTAVEHOME}}), and @var{version} |
|
239 is the version number of Octave. This file is provided so that changes |
|
240 to the default Octave environment can be made globally for all users for |
|
241 a particular version of Octave. Some care should be taken when making |
|
242 changes to this file, since all users of Octave at your site will be |
|
243 affected. |
|
244 |
|
245 @item ~/.octaverc |
|
246 @cindex @code{~/.octaverc} |
|
247 This file is normally used to make personal changes to the default |
|
248 Octave environment. |
|
249 |
|
250 @item .octaverc |
|
251 @cindex @code{.octaverc} |
|
252 This file can be used to make changes to the default Octave environment |
|
253 for a particular project. Octave searches for this file in the current |
|
254 directory after it reads @file{~/.octaverc}. Any use of the @code{cd} |
|
255 command in the @file{~/.octaverc} file will affect the directory that |
|
256 Octave searches for the file @file{.octaverc}. |
|
257 |
|
258 If you start Octave in your home directory, commands from from the file |
|
259 @file{~/.octaverc} will only be executed once. |
|
260 @end table |
|
261 |
|
262 A message will be displayed as each of the startup files is read if you |
|
263 invoke Octave with the @code{--verbose} option but without the |
|
264 @code{--silent} option. |
|
265 |
|
266 Startup files may contain any valid Octave commands, including function |
|
267 definitions. |
|
268 |
4167
|
269 @node Quitting Octave |
3294
|
270 @section Quitting Octave |
|
271 @cindex exiting octave |
|
272 @cindex quitting octave |
|
273 |
3332
|
274 @DOCSTRING(quit) |
3294
|
275 |
3332
|
276 @DOCSTRING(atexit) |
3294
|
277 |
4167
|
278 @node Getting Help |
3294
|
279 @section Commands for Getting Help |
|
280 @cindex on-line help |
|
281 @cindex help, on-line |
|
282 |
|
283 The entire text of this manual is available from the Octave prompt |
|
284 via the command @kbd{help -i}. In addition, the documentation for |
|
285 individual user-written functions and variables is also available via |
|
286 the @kbd{help} command. This section describes the commands used for |
|
287 reading the manual and the documentation strings for user-supplied |
|
288 functions and variables. @xref{Function Files}, for more information |
|
289 about how to document the functions you write. |
|
290 |
3332
|
291 @DOCSTRING(help) |
3294
|
292 |
|
293 The help command can give you information about operators, but not the |
|
294 comma and semicolons that are used as command separators. To get help |
|
295 for those, you must type @kbd{help comma} or @kbd{help semicolon}. |
|
296 |
3332
|
297 @DOCSTRING(INFO_FILE) |
3294
|
298 |
3332
|
299 @DOCSTRING(INFO_PROGRAM) |
3294
|
300 |
3686
|
301 @DOCSTRING(MAKEINFO_PROGRAM) |
|
302 |
3332
|
303 @DOCSTRING(suppress_verbose_help_message) |
3294
|
304 |
4167
|
305 @node Command Line Editing |
3294
|
306 @section Command Line Editing |
|
307 @cindex command-line editing |
|
308 @cindex editing the command line |
|
309 |
|
310 Octave uses the GNU readline library to provide an extensive set of |
|
311 command-line editing and history features. Only the most common |
|
312 features are described in this manual. Please see The GNU Readline |
|
313 Library manual for more information. |
|
314 |
|
315 To insert printing characters (letters, digits, symbols, etc.), simply |
|
316 type the character. Octave will insert the character at the cursor and |
|
317 advance the cursor forward. |
|
318 |
|
319 Many of the command-line editing functions operate using control |
|
320 characters. For example, the character @kbd{Control-a} moves the cursor |
|
321 to the beginning of the line. To type @kbd{C-a}, hold down @key{CTRL} |
|
322 and then press @key{a}. In the following sections, control characters |
|
323 such as @kbd{Control-a} are written as @kbd{C-a}. |
|
324 |
|
325 Another set of command-line editing functions use Meta characters. On |
|
326 some terminals, you type @kbd{M-u} by holding down @key{META} and |
|
327 pressing @key{u}. If your terminal does not have a @key{META} key, you |
|
328 can still type Meta charcters using two-character sequences starting |
|
329 with @kbd{ESC}. Thus, to enter @kbd{M-u}, you could type |
|
330 @key{ESC}@key{u}. The @kbd{ESC} character sequences are also allowed on |
|
331 terminals with real Meta keys. In the following sections, Meta |
|
332 characters such as @kbd{Meta-u} are written as @kbd{M-u}. |
|
333 |
3428
|
334 |
3294
|
335 @menu |
|
336 * Cursor Motion:: |
|
337 * Killing and Yanking:: |
|
338 * Commands For Text:: |
|
339 * Commands For Completion:: |
|
340 * Commands For History:: |
3439
|
341 * Customizing readline:: |
3294
|
342 * Customizing the Prompt:: |
|
343 * Diary and Echo Commands:: |
|
344 @end menu |
|
345 |
4167
|
346 @node Cursor Motion |
3294
|
347 @subsection Cursor Motion |
|
348 |
|
349 The following commands allow you to position the cursor. |
|
350 |
|
351 @table @kbd |
|
352 @item C-b |
|
353 Move back one character. |
|
354 |
|
355 @item C-f |
|
356 Move forward one character. |
|
357 |
|
358 @item @key{DEL} |
|
359 Delete the character to the left of the cursor. |
|
360 |
|
361 @item C-d |
|
362 Delete the character underneath the cursor. |
|
363 |
|
364 @item M-f |
|
365 Move forward a word. |
|
366 |
|
367 @item M-b |
|
368 Move backward a word. |
|
369 |
|
370 @item C-a |
|
371 Move to the start of the line. |
|
372 |
|
373 @item C-e |
|
374 Move to the end of the line. |
|
375 |
|
376 @item C-l |
|
377 Clear the screen, reprinting the current line at the top. |
|
378 |
|
379 @item C-_ |
|
380 @itemx C-/ |
|
381 Undo the last thing that you did. You can undo all the way back to an |
|
382 empty line. |
|
383 |
|
384 @item M-r |
|
385 Undo all changes made to this line. This is like typing the `undo' |
|
386 command enough times to get back to the beginning. |
|
387 @end table |
|
388 |
|
389 The above table describes the most basic possible keystrokes that you need |
|
390 in order to do editing of the input line. On most terminals, you can |
|
391 also use the arrow keys in place of @kbd{C-f} and @kbd{C-b} to move |
|
392 forward and backward. |
|
393 |
|
394 Notice how @kbd{C-f} moves forward a character, while @kbd{M-f} moves |
|
395 forward a word. It is a loose convention that control keystrokes |
|
396 operate on characters while meta keystrokes operate on words. |
|
397 |
|
398 There is also a function available so that you can clear the screen from |
|
399 within Octave programs. |
|
400 |
|
401 @cindex clearing the screen |
|
402 |
3332
|
403 @DOCSTRING(clc) |
3294
|
404 |
4167
|
405 @node Killing and Yanking |
3294
|
406 @subsection Killing and Yanking |
|
407 |
|
408 @dfn{Killing} text means to delete the text from the line, but to save |
|
409 it away for later use, usually by @dfn{yanking} it back into the line. |
|
410 If the description for a command says that it `kills' text, then you can |
|
411 be sure that you can get the text back in a different (or the same) |
|
412 place later. |
|
413 |
|
414 Here is the list of commands for killing text. |
|
415 |
|
416 @table @kbd |
|
417 @item C-k |
|
418 Kill the text from the current cursor position to the end of the line. |
|
419 |
|
420 @item M-d |
|
421 Kill from the cursor to the end of the current word, or if between |
|
422 words, to the end of the next word. |
|
423 |
|
424 @item M-@key{DEL} |
|
425 Kill from the cursor to the start of the previous word, or if between |
|
426 words, to the start of the previous word. |
|
427 |
|
428 @item C-w |
|
429 Kill from the cursor to the previous whitespace. This is different than |
|
430 @kbd{M-@key{DEL}} because the word boundaries differ. |
|
431 @end table |
|
432 |
|
433 And, here is how to @dfn{yank} the text back into the line. Yanking |
|
434 means to copy the most-recently-killed text from the kill buffer. |
|
435 |
|
436 @table @kbd |
|
437 @item C-y |
|
438 Yank the most recently killed text back into the buffer at the cursor. |
|
439 |
|
440 @item M-y |
|
441 Rotate the kill-ring, and yank the new top. You can only do this if |
|
442 the prior command is @kbd{C-y} or @kbd{M-y}. |
|
443 @end table |
|
444 |
|
445 When you use a kill command, the text is saved in a @dfn{kill-ring}. |
|
446 Any number of consecutive kills save all of the killed text together, so |
|
447 that when you yank it back, you get it in one clean sweep. The kill |
|
448 ring is not line specific; the text that you killed on a previously |
|
449 typed line is available to be yanked back later, when you are typing |
|
450 another line. |
|
451 |
4167
|
452 @node Commands For Text |
3294
|
453 @subsection Commands For Changing Text |
|
454 |
|
455 The following commands can be used for entering characters that would |
|
456 otherwise have a special meaning (e.g., @kbd{TAB}, @kbd{C-q}, etc.), or |
|
457 for quickly correcting typing mistakes. |
|
458 |
|
459 @table @kbd |
|
460 @item C-q |
|
461 @itemx C-v |
|
462 Add the next character that you type to the line verbatim. This is |
|
463 how to insert things like @kbd{C-q} for example. |
|
464 |
|
465 @item M-@key{TAB} |
|
466 Insert a tab character. |
|
467 |
|
468 @item C-t |
|
469 Drag the character before the cursor forward over the character at the |
|
470 cursor, also moving the cursor forward. If the cursor is at the end of |
|
471 the line, then transpose the two characters before it. |
|
472 |
|
473 @item M-t |
|
474 Drag the word behind the cursor past the word in front of the cursor |
|
475 moving the cursor over that word as well. |
|
476 |
|
477 @item M-u |
|
478 Uppercase the characters following the cursor to the end of the current |
|
479 (or following) word, moving the cursor to the end of the word. |
|
480 |
|
481 @item M-l |
|
482 Lowecase the characters following the cursor to the end of the current |
|
483 (or following) word, moving the cursor to the end of the word. |
|
484 |
|
485 @item M-c |
|
486 Uppercase the character following the cursor (or the beginning of the |
|
487 next word if the cursor is between words), moving the cursor to the end |
|
488 of the word. |
|
489 @end table |
|
490 |
4167
|
491 @node Commands For Completion |
3294
|
492 @subsection Letting Readline Type For You |
|
493 @cindex command completion |
|
494 |
|
495 The following commands allow Octave to complete command and variable |
|
496 names for you. |
|
497 |
|
498 @table @kbd |
|
499 @item @key{TAB} |
|
500 Attempt to do completion on the text before the cursor. Octave can |
|
501 complete the names of commands and variables. |
|
502 |
|
503 @item M-? |
|
504 List the possible completions of the text before the cursor. |
|
505 @end table |
|
506 |
3332
|
507 @DOCSTRING(completion_append_char) |
3294
|
508 |
3332
|
509 @DOCSTRING(completion_matches) |
3294
|
510 |
4167
|
511 @node Commands For History |
3294
|
512 @subsection Commands For Manipulating The History |
|
513 @cindex command history |
|
514 @cindex input history |
|
515 @cindex history of commands |
|
516 |
|
517 Octave normally keeps track of the commands you type so that you can |
|
518 recall previous commands to edit or execute them again. When you exit |
|
519 Octave, the most recent commands you have typed, up to the number |
|
520 specified by the variable @code{history_size}, are saved in a file. |
|
521 When Octave starts, it loads an initial list of commands from the file |
|
522 named by the variable @code{history_file}. |
|
523 |
|
524 Here are the commands for simple browsing and searching the history |
|
525 list. |
|
526 |
|
527 @table @kbd |
|
528 @item @key{LFD} |
|
529 @itemx @key{RET} |
|
530 Accept the line regardless of where the cursor is. If this line is |
|
531 non-empty, add it to the history list. If this line was a history |
|
532 line, then restore the history line to its original state. |
|
533 |
|
534 @item C-p |
|
535 Move `up' through the history list. |
|
536 |
|
537 @item C-n |
|
538 Move `down' through the history list. |
|
539 |
|
540 @item M-< |
|
541 Move to the first line in the history. |
|
542 |
|
543 @item M-> |
|
544 Move to the end of the input history, i.e., the line you are entering! |
|
545 |
|
546 @item C-r |
|
547 Search backward starting at the current line and moving `up' through |
|
548 the history as necessary. This is an incremental search. |
|
549 |
|
550 @item C-s |
|
551 Search forward starting at the current line and moving `down' through |
|
552 the history as necessary. |
|
553 @end table |
|
554 |
|
555 On most terminals, you can also use the arrow keys in place of @kbd{C-p} |
|
556 and @kbd{C-n} to move through the history list. |
|
557 |
|
558 In addition to the keyboard commands for moving through the history |
|
559 list, Octave provides three functions for viewing, editing, and |
|
560 re-running chunks of commands from the history list. |
|
561 |
3332
|
562 @DOCSTRING(history) |
3294
|
563 |
3332
|
564 @DOCSTRING(edit_history) |
3294
|
565 |
3332
|
566 @DOCSTRING(run_history) |
3294
|
567 |
3368
|
568 @DOCSTRING(EDITOR) |
3294
|
569 |
3332
|
570 @DOCSTRING(history_file) |
3294
|
571 |
3332
|
572 @DOCSTRING(history_size) |
3294
|
573 |
3332
|
574 @DOCSTRING(saving_history) |
3294
|
575 |
4167
|
576 @node Customizing readline |
3428
|
577 @subsection Customizing @code{readline} |
|
578 |
|
579 @c XXX FIXME XXX -- need a brief description of the ~/.inputrc file here. |
|
580 |
3439
|
581 @DOCSTRING(read_readline_init_file) |
3428
|
582 |
4167
|
583 @node Customizing the Prompt |
3294
|
584 @subsection Customizing the Prompt |
|
585 @cindex prompt customization |
|
586 @cindex customizing the prompt |
|
587 |
|
588 The following variables are available for customizing the appearance of |
|
589 the command-line prompts. Octave allows the prompt to be customized by |
|
590 inserting a number of backslash-escaped special characters that are |
|
591 decoded as follows: |
|
592 |
|
593 @table @samp |
|
594 @item \t |
|
595 The time. |
|
596 |
|
597 @item \d |
|
598 The date. |
|
599 |
|
600 @item \n |
|
601 Begins a new line by printing the equivalent of a carriage return |
|
602 followed by a line feed. |
|
603 |
|
604 @item \s |
|
605 The name of the program (usually just @samp{octave}). |
|
606 |
|
607 @item \w |
|
608 The current working directory. |
|
609 |
|
610 @item \W |
|
611 The basename of the current working directory. |
|
612 |
|
613 @item \u |
|
614 The username of the current user. |
|
615 |
|
616 @item \h |
|
617 The hostname, up to the first `.'. |
|
618 |
|
619 @item \H |
|
620 The hostname. |
|
621 |
|
622 @item \# |
|
623 The command number of this command, counting from when Octave starts. |
|
624 |
|
625 @item \! |
|
626 The history number of this command. This differs from @samp{\#} by the |
|
627 number of commands in the history list when Octave starts. |
|
628 |
|
629 @item \$ |
|
630 If the effective UID is 0, a @samp{#}, otherwise a @samp{$}. |
|
631 |
|
632 @item \nnn |
|
633 The character whose character code in octal is @var{nnn}. |
|
634 |
|
635 @item \\ |
|
636 A backslash. |
|
637 @end table |
|
638 |
3332
|
639 @DOCSTRING(PS1) |
3294
|
640 |
3332
|
641 @DOCSTRING(PS2) |
3294
|
642 |
3332
|
643 @DOCSTRING(PS4) |
3294
|
644 |
4167
|
645 @node Diary and Echo Commands |
3294
|
646 @subsection Diary and Echo Commands |
|
647 @cindex diary of commands and output |
|
648 @cindex command and ouput logs |
|
649 @cindex logging commands and output |
|
650 @cindex echoing executing commands |
|
651 @cindex command echoing |
|
652 |
|
653 Octave's diary feature allows you to keep a log of all or part of an |
|
654 interactive session by recording the input you type and the output that |
|
655 Octave produces in a separate file. |
|
656 |
3332
|
657 @DOCSTRING(diary) |
3294
|
658 |
|
659 Sometimes it is useful to see the commands in a function or script as |
|
660 they are being evaluated. This can be especially helpful for debugging |
|
661 some kinds of problems. |
|
662 |
3332
|
663 @DOCSTRING(echo) |
3294
|
664 |
3332
|
665 @DOCSTRING(echo_executing_commands) |
3294
|
666 |
4167
|
667 @node Errors |
3294
|
668 @section How Octave Reports Errors |
|
669 @cindex error messages |
|
670 @cindex messages, error |
|
671 |
|
672 Octave reports two kinds of errors for invalid programs. |
|
673 |
|
674 A @dfn{parse error} occurs if Octave cannot understand something you |
|
675 have typed. For example, if you misspell a keyword, |
|
676 |
|
677 @example |
|
678 octave:13> functon y = f (x) y = x^2; endfunction |
|
679 @end example |
|
680 |
|
681 @noindent |
|
682 Octave will respond immediately with a message like this: |
|
683 |
|
684 @example |
|
685 parse error: |
|
686 |
|
687 functon y = f (x) y = x^2; endfunction |
|
688 ^ |
|
689 @end example |
|
690 |
|
691 @noindent |
|
692 For most parse errors, Octave uses a caret (@samp{^}) to mark the point |
|
693 on the line where it was unable to make sense of your input. In this |
|
694 case, Octave generated an error message because the keyword |
|
695 @code{function} was misspelled. Instead of seeing @samp{function f}, |
|
696 Octave saw two consecutive variable names, which is invalid in this |
|
697 context. It marked the error at @code{y} because the first name by |
|
698 itself was accepted as valid input. |
|
699 |
|
700 Another class of error message occurs at evaluation time. These |
|
701 errors are called @dfn{run-time errors}, or sometimes |
|
702 @dfn{evaluation errors} because they occur when your program is being |
|
703 @dfn{run}, or @dfn{evaluated}. For example, if after correcting the |
|
704 mistake in the previous function definition, you type |
|
705 |
|
706 @example |
|
707 octave:13> f () |
|
708 @end example |
|
709 |
|
710 @noindent |
|
711 Octave will respond with |
|
712 |
|
713 @example |
|
714 @group |
|
715 error: `x' undefined near line 1 column 24 |
|
716 error: evaluating expression near line 1, column 24 |
|
717 error: evaluating assignment expression near line 1, column 22 |
|
718 error: called from `f' |
|
719 @end group |
|
720 @end example |
|
721 |
|
722 This error message has several parts, and gives you quite a bit of |
|
723 information to help you locate the source of the error. The messages |
|
724 are generated from the point of the innermost error, and provide a |
|
725 traceback of enclosing expressions and function calls. |
|
726 |
|
727 In the example above, the first line indicates that a variable named |
|
728 @samp{x} was found to be undefined near line 1 and column 24 of some |
|
729 function or expression. For errors occurring within functions, lines |
|
730 are counted from the beginning of the file containing the function |
|
731 definition. For errors occurring at the top level, the line number |
|
732 indicates the input line number, which is usually displayed in the |
|
733 prompt string. |
|
734 |
|
735 The second and third lines in the example indicate that the error |
|
736 occurred within an assignment expression, and the last line of the error |
|
737 message indicates that the error occurred within the function @code{f}. |
|
738 If the function @code{f} had been called from another function, for |
|
739 example, @code{g}, the list of errors would have ended with one more |
|
740 line: |
|
741 |
|
742 @example |
|
743 error: called from `g' |
|
744 @end example |
|
745 |
|
746 These lists of function calls usually make it fairly easy to trace the |
|
747 path your program took before the error occurred, and to correct the |
|
748 error before trying again. |
|
749 |
4167
|
750 @node Executable Octave Programs |
3294
|
751 @section Executable Octave Programs |
|
752 @cindex executable scripts |
|
753 @cindex scripts |
|
754 @cindex self contained programs |
|
755 @cindex program, self contained |
|
756 @cindex @samp{#!} |
|
757 |
|
758 Once you have learned Octave, you may want to write self-contained |
|
759 Octave scripts, using the @samp{#!} script mechanism. You can do this |
|
760 on GNU systems and on many Unix systems @footnote{The @samp{#!} |
|
761 mechanism works on Unix systems derived from Berkeley Unix, System V |
|
762 Release 4, and some System V Release 3 systems.} |
|
763 |
|
764 For example, you could create a text file named @file{hello}, containing |
|
765 the following lines: |
|
766 |
|
767 @example |
|
768 @group |
|
769 #! @var{octave-interpreter-name} -qf |
|
770 # a sample Octave program |
|
771 printf ("Hello, world!\n"); |
|
772 @end group |
|
773 @end example |
|
774 |
|
775 @noindent |
|
776 (where @var{octave-interpreter-name} should be replaced with the full |
|
777 file name for your Octave binary). After making this file executable |
|
778 (with the @code{chmod} command), you can simply type: |
|
779 |
|
780 @example |
|
781 hello |
|
782 @end example |
|
783 |
|
784 @noindent |
|
785 at the shell, and the system will arrange to run Octave as if you had |
|
786 typed: |
|
787 |
|
788 @example |
|
789 octave hello |
|
790 @end example |
|
791 |
|
792 The line beginning with @samp{#!} lists the full file name of an |
|
793 interpreter to be run, and an optional initial command line argument to |
|
794 pass to that interpreter. The operating system then runs the |
|
795 interpreter with the given argument and the full argument list of the |
|
796 executed program. The first argument in the list is the full file name |
|
797 of the Octave program. The rest of the argument list will either be |
|
798 options to Octave, or data files, or both. The @samp{-qf} option is |
|
799 usually specified in stand-alone Octave programs to prevent them from |
|
800 printing the normal startup message, and to keep them from behaving |
|
801 differently depending on the contents of a particular user's |
|
802 @file{~/.octaverc} file. @xref{Invoking Octave}. Note that some |
|
803 operating systems may place a limit on the number of characters that are |
|
804 recognized after @samp{#!}. |
|
805 |
|
806 Self-contained Octave scripts are useful when you want to write a |
|
807 program which users can invoke without knowing that the program is |
|
808 written in the Octave language. |
|
809 |
|
810 If you invoke an executable Octave script with command line arguments, |
|
811 the arguments are available in the built-in variable @code{argv}. |
|
812 @xref{Command Line Options}. For example, the following program will |
|
813 reproduce the command line that is used to execute it. |
|
814 |
|
815 @example |
|
816 @group |
|
817 #! /bin/octave -qf |
|
818 printf ("%s", program_name); |
|
819 for i = 1:nargin |
3974
|
820 printf (" %s", argv@{i@}); |
3294
|
821 endfor |
|
822 printf ("\n"); |
|
823 @end group |
|
824 @end example |
|
825 |
4167
|
826 @node Comments |
3294
|
827 @section Comments in Octave Programs |
|
828 @cindex @samp{#} |
|
829 @cindex @samp{%} |
|
830 @cindex comments |
|
831 @cindex use of comments |
|
832 @cindex documenting Octave programs |
|
833 @cindex programs |
|
834 |
|
835 A @dfn{comment} is some text that is included in a program for the sake |
|
836 of human readers, and that is not really part of the program. Comments |
|
837 can explain what the program does, and how it works. Nearly all |
|
838 programming languages have provisions for comments, because programs are |
|
839 typically hard to understand without them. |
|
840 |
|
841 In the Octave language, a comment starts with either the sharp sign |
|
842 character, @samp{#}, or the percent symbol @samp{%} and continues to the |
|
843 end of the line. The Octave interpreter ignores the rest of a |
|
844 line following a sharp sign or percent symbol. For example, we could |
|
845 have put the following into the function @code{f}: |
|
846 |
|
847 @example |
|
848 @group |
|
849 function xdot = f (x, t) |
|
850 |
|
851 # usage: f (x, t) |
|
852 # |
|
853 # This function defines the right hand |
|
854 # side functions for a set of nonlinear |
|
855 # differential equations. |
|
856 |
|
857 r = 0.25; |
|
858 @dots{} |
|
859 endfunction |
|
860 @end group |
|
861 @end example |
|
862 |
|
863 The @code{help} command (@pxref{Getting Help}) is able to find the first |
|
864 block of comments in a function (even those that are composed directly |
|
865 on the command line). This means that users of Octave can use the same |
|
866 commands to get help for built-in functions, and for functions that you |
|
867 have defined. For example, after defining the function @code{f} above, |
|
868 the command @kbd{help f} produces the output |
|
869 |
|
870 @example |
|
871 @group |
|
872 usage: f (x, t) |
|
873 |
|
874 This function defines the right hand |
|
875 side functions for a set of nonlinear |
|
876 differential equations. |
|
877 @end group |
|
878 @end example |
|
879 |
|
880 Although it is possible to put comment lines into keyboard-composed |
|
881 throw-away Octave programs, it usually isn't very useful, because the |
|
882 purpose of a comment is to help you or another person understand the |
|
883 program at a later time. |
|
884 |