annotate doc/interpreter/io.txi @ 20628:3af34e1ef330

Preliminary inclusion of uixx objects properties in the manual (bug #46076) * doc/interpreter/genpropdoc.m: add uixx objects to the list of supported graphics objects * doc/interpreter/genpropdoc.m (get_doc): add uixx objects and their specific properties (currently empty documentation) * doc/interpreter/plot.txi("Interacting with Plots"): add a note and a reference about ui* family of functions. * doc/interpreter/plot.txi("Interacting with Plots"): for consistency, remove "uimenu" reference. All the other uixx are already in the gui section * doc/interpreter/plot.txi("graphics data structure"): add uixx objects * doc/interpreter/gui.txi("UI Elements"): add "uimenu" function reference * doc/module.mk: add rules to build uixx properties texi files. * graphics.in.h: make uixx "__object__" property (Octave internal) hidden so that it does not appear in the documentation.
author Pantxo Diribarne <pantxo.diribarne@gmail.com>
date Fri, 09 Oct 2015 16:25:27 +0200
parents e51473fdb622
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19731
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 19630
diff changeset
1 @c Copyright (C) 1996-2015 John W. Eaton
7018
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
2 @c
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
3 @c This file is part of Octave.
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
4 @c
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
5 @c Octave is free software; you can redistribute it and/or modify it
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
6 @c under the terms of the GNU General Public License as published by the
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
7 @c Free Software Foundation; either version 3 of the License, or (at
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
8 @c your option) any later version.
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
9 @c
7018
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
10 @c Octave is distributed in the hope that it will be useful, but WITHOUT
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
11 @c ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
12 @c FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
13 @c for more details.
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
14 @c
7018
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
15 @c You should have received a copy of the GNU General Public License
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
16 @c along with Octave; see the file COPYING. If not, see
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
17 @c <http://www.gnu.org/licenses/>.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
18
4169
d5667eaf8859 [project @ 2002-11-12 03:13:42 by jwe]
jwe
parents: 4167
diff changeset
19 @node Input and Output
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
20 @chapter Input and Output
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
21
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
22 Octave supports several ways of reading and writing data to or from the
8828
8463d1a2e544 Doc fixes.
Brian Gough <bjg@network-theory.co.uk>
parents: 8817
diff changeset
23 prompt or a file. The simplest functions for data Input and Output
12546
39ca02387a32 Improve docstrings for a number of functions.
Rik <octave@nomad.inbox5.com>
parents: 12522
diff changeset
24 (I/O) are easy to use, but only provide limited control of how
16826
a4969508008e doc: Periodic spellcheck of the documentation.
Rik <rik@octave.org>
parents: 16216
diff changeset
25 data is processed. For more control, a set of functions modeled
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
26 after the C standard library are also provided by Octave.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
27
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
28 @menu
17152
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16875
diff changeset
29 * Basic Input and Output::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16875
diff changeset
30 * C-Style I/O Functions::
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
31 @end menu
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
32
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
33 @node Basic Input and Output
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
34 @section Basic Input and Output
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
35
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
36 @c We could use a two-line introduction here...
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
37
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
38 @menu
17152
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16875
diff changeset
39 * Terminal Output::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16875
diff changeset
40 * Terminal Input::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16875
diff changeset
41 * Simple File I/O::
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
42 @end menu
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
43
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
44 @node Terminal Output
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
45 @subsection Terminal Output
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
46
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
47 Since Octave normally prints the value of an expression as soon as it
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
48 has been evaluated, the simplest of all I/O functions is a simple
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
49 expression. For example, the following expression will display the
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
50 value of @samp{pi}
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
51
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
52 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9039
diff changeset
53 @group
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
54 pi
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
55 @print{} pi = 3.1416
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9039
diff changeset
56 @end group
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
57 @end example
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
58
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
59 This works well as long as it is acceptable to have the name of the
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
60 variable (or @samp{ans}) printed along with the value. To print the
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
61 value of a variable without printing its name, use the function
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
62 @code{disp}.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
63
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
64 The @code{format} command offers some control over the way Octave prints
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
65 values with @code{disp} and through the normal echoing mechanism.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
66
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
67 @DOCSTRING(disp)
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
68
12562
c686d2be0102 Add list_in_columns, terminal_size functions to documentation.
Rik <octave@nomad.inbox5.com>
parents: 12546
diff changeset
69 @DOCSTRING(list_in_columns)
c686d2be0102 Add list_in_columns, terminal_size functions to documentation.
Rik <octave@nomad.inbox5.com>
parents: 12546
diff changeset
70
c686d2be0102 Add list_in_columns, terminal_size functions to documentation.
Rik <octave@nomad.inbox5.com>
parents: 12546
diff changeset
71 @DOCSTRING(terminal_size)
c686d2be0102 Add list_in_columns, terminal_size functions to documentation.
Rik <octave@nomad.inbox5.com>
parents: 12546
diff changeset
72
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
73 @DOCSTRING(format)
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
74
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
75 @menu
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
76 * Paging Screen Output::
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
77 @end menu
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
78
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
79 @node Paging Screen Output
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
80 @subsubsection Paging Screen Output
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
81
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
82 When running interactively, Octave normally sends any output intended
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
83 for your terminal that is more than one screen long to a paging program,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
84 such as @code{less} or @code{more}. This avoids the problem of having a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
85 large volume of output stream by before you can read it. With
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
86 @code{less} (and some versions of @code{more}) you can also scan forward
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
87 and backward, and search for specific items.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
88
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
89 Normally, no output is displayed by the pager until just before Octave
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
90 is ready to print the top level prompt, or read from the standard input
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
91 (for example, by using the @code{fscanf} or @code{scanf} functions).
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
92 This means that there may be some delay before any output appears on
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
93 your screen if you have asked Octave to perform a significant amount of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
94 work with a single command statement. The function @code{fflush} may be
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
95 used to force output to be sent to the pager (or any other stream)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
96 immediately.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
97
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 9701
diff changeset
98 You can select the program to run as the pager using the @env{PAGER}
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
99 function, and you can turn paging off by using the function
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
100 @code{more}.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
101
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
102 @DOCSTRING(more)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
103
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
104 @DOCSTRING(PAGER)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
105
6549
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 5775
diff changeset
106 @DOCSTRING(PAGER_FLAGS)
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 5775
diff changeset
107
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
108 @DOCSTRING(page_screen_output)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
109
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
110 @DOCSTRING(page_output_immediately)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
111
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
112 @DOCSTRING(fflush)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
113
5775
ace8d8d26933 [project @ 2006-04-24 19:13:06 by jwe]
jwe
parents: 5287
diff changeset
114 @c FIXME -- maybe this would be a good place to describe the
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
115 @c following message:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
116 @c
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
117 @c warning: connection to external pager (pid = 9334) lost --
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
118 @c warning: pending computations and output may be lost
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
119 @c warning: broken pipe
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
120
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
121 @node Terminal Input
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
122 @subsection Terminal Input
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
123
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
124 Octave has three functions that make it easy to prompt users for
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
125 input. The @code{input} and @code{menu} functions are normally
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
126 used for managing an interactive dialog with a user, and the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
127 @code{keyboard} function is normally used for doing simple debugging.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
128
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
129 @DOCSTRING(input)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
130
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
131 @DOCSTRING(menu)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
132
8817
03b7f618ab3d include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents: 8569
diff changeset
133 @DOCSTRING(yes_or_no)
03b7f618ab3d include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents: 8569
diff changeset
134
6646
bd0a70c3f2db [project @ 2007-05-22 02:27:43 by jwe]
jwe
parents: 6556
diff changeset
135 For @code{input}, the normal command line history and editing functions
bd0a70c3f2db [project @ 2007-05-22 02:27:43 by jwe]
jwe
parents: 6556
diff changeset
136 are available at the prompt.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
137
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
138 Octave also has a function that makes it possible to get a single
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
139 character from the keyboard without requiring the user to type a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
140 carriage return.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
141
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
142 @DOCSTRING(kbhit)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
143
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
144 @node Simple File I/O
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
145 @subsection Simple File I/O
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
146
5225
6434e63553f0 [project @ 2005-03-17 17:53:01 by jwe]
jwe
parents: 5213
diff changeset
147 @cindex saving data
6434e63553f0 [project @ 2005-03-17 17:53:01 by jwe]
jwe
parents: 5213
diff changeset
148 @cindex loading data
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
149 The @code{save} and @code{load} commands allow data to be written to and
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
150 read from disk files in various formats. The default format of files
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
151 written by the @code{save} command can be controlled using the functions
16875
b04ae15530fc Rename default_save_options() to save_default_options().
Rik <rik@octave.org>
parents: 16826
diff changeset
152 @code{save_default_options} and @code{save_precision}.
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
153
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
154 As an example the following code creates a 3-by-3 matrix and saves it
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
155 to the file @samp{myfile.mat}.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
156
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
157 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9039
diff changeset
158 @group
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
159 A = [ 1:3; 4:6; 7:9 ];
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
160 save myfile.mat A
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9039
diff changeset
161 @end group
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
162 @end example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
163
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
164 Once one or more variables have been saved to a file, they can be
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
165 read into memory using the @code{load} command.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
166
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
167 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9039
diff changeset
168 @group
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
169 load myfile.mat
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
170 A
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
171 @print{} A =
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
172 @print{}
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
173 @print{} 1 2 3
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
174 @print{} 4 5 6
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
175 @print{} 7 8 9
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9039
diff changeset
176 @end group
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
177 @end example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
178
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
179 @DOCSTRING(save)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
180
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
181 There are three functions that modify the behavior of @code{save}.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
182
16875
b04ae15530fc Rename default_save_options() to save_default_options().
Rik <rik@octave.org>
parents: 16826
diff changeset
183 @DOCSTRING(save_default_options)
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
184
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
185 @DOCSTRING(save_precision)
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
186
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
187 @DOCSTRING(save_header_format_string)
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
188
16214
ee041a93c755 put save functions text after save command
Michael Godfrey <michaeldgodfrey@gmail.com>
parents: 15466
diff changeset
189 @DOCSTRING(load)
ee041a93c755 put save functions text after save command
Michael Godfrey <michaeldgodfrey@gmail.com>
parents: 15466
diff changeset
190
ee041a93c755 put save functions text after save command
Michael Godfrey <michaeldgodfrey@gmail.com>
parents: 15466
diff changeset
191 @DOCSTRING(fileread)
ee041a93c755 put save functions text after save command
Michael Godfrey <michaeldgodfrey@gmail.com>
parents: 15466
diff changeset
192
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
193 @DOCSTRING(native_float_format)
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
194
8828
8463d1a2e544 Doc fixes.
Brian Gough <bjg@network-theory.co.uk>
parents: 8817
diff changeset
195 It is possible to write data to a file in a similar way to the
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
196 @code{disp} function for writing data to the screen. The @code{fdisp}
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
197 works just like @code{disp} except its first argument is a file pointer
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
198 as created by @code{fopen}. As an example, the following code writes
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
199 to data @samp{myfile.txt}.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
200
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
201 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9039
diff changeset
202 @group
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
203 fid = fopen ("myfile.txt", "w");
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
204 fdisp (fid, "3/8 is ");
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
205 fdisp (fid, 3/8);
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
206 fclose (fid);
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9039
diff changeset
207 @end group
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
208 @end example
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
209
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
210 @noindent
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
211 @xref{Opening and Closing Files}, for details on how to use @code{fopen}
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
212 and @code{fclose}.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
213
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
214 @DOCSTRING(fdisp)
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
215
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents: 7336
diff changeset
216 Octave can also read and write matrices text files such as comma
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents: 7336
diff changeset
217 separated lists.
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents: 7336
diff changeset
218
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents: 7336
diff changeset
219 @DOCSTRING(dlmwrite)
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents: 7336
diff changeset
220
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents: 7336
diff changeset
221 @DOCSTRING(dlmread)
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents: 7336
diff changeset
222
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents: 7336
diff changeset
223 @DOCSTRING(csvwrite)
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents: 7336
diff changeset
224
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents: 7336
diff changeset
225 @DOCSTRING(csvread)
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents: 7336
diff changeset
226
11148
c9ec943ebe65 doc fix
Ben Abbott <bpabbott@mac.com>
parents: 11147
diff changeset
227 Formatted data from can be read from, or written to, text files as well.
c9ec943ebe65 doc fix
Ben Abbott <bpabbott@mac.com>
parents: 11147
diff changeset
228
11147
a81994607ca0 document textscan and textread
John W. Eaton <jwe@octave.org>
parents: 11142
diff changeset
229 @DOCSTRING(textread)
a81994607ca0 document textscan and textread
John W. Eaton <jwe@octave.org>
parents: 11142
diff changeset
230
a81994607ca0 document textscan and textread
John W. Eaton <jwe@octave.org>
parents: 11142
diff changeset
231 @DOCSTRING(textscan)
a81994607ca0 document textscan and textread
John W. Eaton <jwe@octave.org>
parents: 11142
diff changeset
232
15547
9a455cf96dbe Incorporate importdata.m into Octave sources
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 15467
diff changeset
233 The @code{importdata} function has the ability to work with a wide
9a455cf96dbe Incorporate importdata.m into Octave sources
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 15467
diff changeset
234 variety of data.
9a455cf96dbe Incorporate importdata.m into Octave sources
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 15467
diff changeset
235
9a455cf96dbe Incorporate importdata.m into Octave sources
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 15467
diff changeset
236 @DOCSTRING(importdata)
9a455cf96dbe Incorporate importdata.m into Octave sources
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 15467
diff changeset
237
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
238 @menu
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
239 * Saving Data on Unexpected Exits::
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
240 @end menu
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
241
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
242 @node Saving Data on Unexpected Exits
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
243 @subsubsection Saving Data on Unexpected Exits
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
244
8347
fa78cb8d8a5c corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents: 8286
diff changeset
245 If Octave for some reason exits unexpectedly it will by default save the
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
246 variables available in the workspace to a file in the current directory.
14889
577df411e0c7 rename octave-core file to octave-workspace
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
247 By default this file is named @samp{octave-workspace} and can be loaded
9039
51dc9691f23f Cleanup documentation files errors.texi, debug.texi, io.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
248 into memory with the @code{load} command. While the default behavior
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
249 most often is reasonable it can be changed through the following
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
250 functions.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
251
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
252 @DOCSTRING(crash_dumps_octave_core)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
253
4449
e2c91da0aa16 [project @ 2003-07-09 02:21:11 by jwe]
jwe
parents: 4328
diff changeset
254 @DOCSTRING(sighup_dumps_octave_core)
e2c91da0aa16 [project @ 2003-07-09 02:21:11 by jwe]
jwe
parents: 4328
diff changeset
255
e2c91da0aa16 [project @ 2003-07-09 02:21:11 by jwe]
jwe
parents: 4328
diff changeset
256 @DOCSTRING(sigterm_dumps_octave_core)
e2c91da0aa16 [project @ 2003-07-09 02:21:11 by jwe]
jwe
parents: 4328
diff changeset
257
5287
f14bdd2bec91 [project @ 2005-04-19 15:15:21 by jwe]
jwe
parents: 5225
diff changeset
258 @DOCSTRING(octave_core_file_options)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
259
6550
1b7a6061a05d [project @ 2007-04-20 07:40:26 by jwe]
jwe
parents: 6549
diff changeset
260 @DOCSTRING(octave_core_file_limit)
1b7a6061a05d [project @ 2007-04-20 07:40:26 by jwe]
jwe
parents: 6549
diff changeset
261
1b7a6061a05d [project @ 2007-04-20 07:40:26 by jwe]
jwe
parents: 6549
diff changeset
262 @DOCSTRING(octave_core_file_name)
1b7a6061a05d [project @ 2007-04-20 07:40:26 by jwe]
jwe
parents: 6549
diff changeset
263
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
264 @node C-Style I/O Functions
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
265 @section C-Style I/O Functions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
266
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
267 Octave's C-style input and output functions provide most of the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
268 functionality of the C programming language's standard I/O library. The
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
269 argument lists for some of the input functions are slightly different,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
270 however, because Octave has no way of passing arguments by reference.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
271
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
272 In the following, @var{file} refers to a file name and @code{fid} refers
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
273 to an integer file number, as returned by @code{fopen}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
274
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
275 There are three files that are always available. Although these files
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
276 can be accessed using their corresponding numeric file ids, you should
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
277 always use the symbolic names given in the table below, since it will
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
278 make your programs easier to understand.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
279
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
280 @DOCSTRING(stdin)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
281
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
282 @DOCSTRING(stdout)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
283
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
284 @DOCSTRING(stderr)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
285
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
286 @menu
17152
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16875
diff changeset
287 * Opening and Closing Files::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16875
diff changeset
288 * Simple Output::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16875
diff changeset
289 * Line-Oriented Input::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16875
diff changeset
290 * Formatted Output::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16875
diff changeset
291 * Output Conversion for Matrices::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16875
diff changeset
292 * Output Conversion Syntax::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16875
diff changeset
293 * Table of Output Conversions::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16875
diff changeset
294 * Integer Conversions::
9032
349616d9c38e Cleanup top-level documentation menu in octave.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
295 * Floating-Point Conversions::
17152
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16875
diff changeset
296 * Other Output Conversions::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16875
diff changeset
297 * Formatted Input::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16875
diff changeset
298 * Input Conversion Syntax::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16875
diff changeset
299 * Table of Input Conversions::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16875
diff changeset
300 * Numeric Input Conversions::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16875
diff changeset
301 * String Input Conversions::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16875
diff changeset
302 * Binary I/O::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16875
diff changeset
303 * Temporary Files::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16875
diff changeset
304 * EOF and Errors::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16875
diff changeset
305 * File Positioning::
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
306 @end menu
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
307
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
308 @node Opening and Closing Files
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
309 @subsection Opening and Closing Files
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
310
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
311 When reading data from a file it must be opened for reading first, and
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
312 likewise when writing to a file. The @code{fopen} function returns a
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
313 pointer to an open file that is ready to be read or written. Once all
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
314 data has been read from or written to the opened file it should be closed.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
315 The @code{fclose} function does this. The following code illustrates
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
316 the basic pattern for writing to a file, but a very similar pattern is
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
317 used when reading a file.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
318
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
319 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9039
diff changeset
320 @group
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
321 filename = "myfile.txt";
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
322 fid = fopen (filename, "w");
9039
51dc9691f23f Cleanup documentation files errors.texi, debug.texi, io.texi
Rik <rdrider0-list@yahoo.com>
parents: 9032
diff changeset
323 # Do the actual I/O here@dots{}
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
324 fclose (fid);
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9039
diff changeset
325 @end group
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
326 @end example
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
327
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
328 @DOCSTRING(fopen)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
329
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
330 @DOCSTRING(fclose)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
331
11142
3450551f591e new function, is_valid_file_id
John W. Eaton <jwe@octave.org>
parents: 10828
diff changeset
332 @DOCSTRING(is_valid_file_id)
3450551f591e new function, is_valid_file_id
John W. Eaton <jwe@octave.org>
parents: 10828
diff changeset
333
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
334 @node Simple Output
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
335 @subsection Simple Output
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
336
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
337 Once a file has been opened for writing a string can be written to the
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
338 file using the @code{fputs} function. The following example shows
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
339 how to write the string @samp{Free Software is needed for Free Science}
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
340 to the file @samp{free.txt}.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
341
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
342 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9039
diff changeset
343 @group
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
344 filename = "free.txt";
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
345 fid = fopen (filename, "w");
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
346 fputs (fid, "Free Software is needed for Free Science");
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
347 fclose (fid);
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9039
diff changeset
348 @end group
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
349 @end example
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
350
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
351 @DOCSTRING(fputs)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
352
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
353 A function much similar to @code{fputs} is available for writing data
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
354 to the screen. The @code{puts} function works just like @code{fputs}
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
355 except it doesn't take a file pointer as its input.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
356
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
357 @DOCSTRING(puts)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
358
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
359 @node Line-Oriented Input
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
360 @subsection Line-Oriented Input
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
361
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
362 To read from a file it must be opened for reading using @code{fopen}.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
363 Then a line can be read from the file using @code{fgetl} as the following
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
364 code illustrates
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
365
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
366 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9039
diff changeset
367 @group
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
368 fid = fopen ("free.txt");
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
369 txt = fgetl (fid)
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
370 @print{} Free Software is needed for Free Science
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
371 fclose (fid);
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9039
diff changeset
372 @end group
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
373 @end example
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
374
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
375 @noindent
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
376 This of course assumes that the file @samp{free.txt} exists and contains
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
377 the line @samp{Free Software is needed for Free Science}.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
378
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
379 @DOCSTRING(fgetl)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
380
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
381 @DOCSTRING(fgets)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
382
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9209
diff changeset
383 @DOCSTRING(fskipl)
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9209
diff changeset
384
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
385 @node Formatted Output
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
386 @subsection Formatted Output
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
387
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
388 This section describes how to call @code{printf} and related functions.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
389
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
390 The following functions are available for formatted output. They are
16826
a4969508008e doc: Periodic spellcheck of the documentation.
Rik <rik@octave.org>
parents: 16216
diff changeset
391 modeled after the C language functions of the same name, but they
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
392 interpret the format template differently in order to improve the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
393 performance of printing vector and matrix values.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
394
20132
1f9ed81bd173 maint: Fix spelling and grammar mistakes in docs and comments (bug #44878)
Rafael Laboissiere <rafael@laboissiere.net>
parents: 20076
diff changeset
395 Implementation Note: For compatibility with @sc{matlab}, escape sequences in
20136
e51473fdb622 doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents: 20132
diff changeset
396 the template string (e.g., @qcode{"@xbackslashchar{}n"} => newline) are
e51473fdb622 doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents: 20132
diff changeset
397 expanded even when the template string is defined with single quotes.
20076
dbf2418a46dd Document expansion of escape sequences in single quotes (bug #44745).
Rik <rik@octave.org>
parents: 19731
diff changeset
398
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
399 @DOCSTRING(printf)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
400
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
401 @DOCSTRING(fprintf)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
402
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
403 @DOCSTRING(sprintf)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
404
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
405 The @code{printf} function can be used to print any number of arguments.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
406 The template string argument you supply in a call provides
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
407 information not only about the number of additional arguments, but also
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
408 about their types and what style should be used for printing them.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
409
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
410 Ordinary characters in the template string are simply written to the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
411 output stream as-is, while @dfn{conversion specifications} introduced by
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
412 a @samp{%} character in the template cause subsequent arguments to be
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
413 formatted and written to the output stream. For example,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
414 @cindex conversion specifications (@code{printf})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
415
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
416 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9039
diff changeset
417 @group
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
418 pct = 37;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
419 filename = "foo.txt";
15466
d174210ce1ec use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
420 printf ("Processed %d%% of '%s'.\nPlease be patient.\n",
7031
120f3135952f [project @ 2007-10-15 15:30:03 by jwe]
jwe
parents: 7018
diff changeset
421 pct, filename);
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9039
diff changeset
422 @end group
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
423 @end example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
424
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
425 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
426 produces output like
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
427
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
428 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9039
diff changeset
429 @group
15466
d174210ce1ec use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
430 Processed 37% of 'foo.txt'.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
431 Please be patient.
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9039
diff changeset
432 @end group
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
433 @end example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
434
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
435 This example shows the use of the @samp{%d} conversion to specify that a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
436 scalar argument should be printed in decimal notation, the @samp{%s}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
437 conversion to specify printing of a string argument, and the @samp{%%}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
438 conversion to print a literal @samp{%} character.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
439
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
440 There are also conversions for printing an integer argument as an
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
441 unsigned value in octal, decimal, or hexadecimal radix (@samp{%o},
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
442 @samp{%u}, or @samp{%x}, respectively); or as a character value
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
443 (@samp{%c}).
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
444
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
445 Floating-point numbers can be printed in normal, fixed-point notation
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
446 using the @samp{%f} conversion or in exponential notation using the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
447 @samp{%e} conversion. The @samp{%g} conversion uses either @samp{%e}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
448 or @samp{%f} format, depending on what is more appropriate for the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
449 magnitude of the particular number.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
450
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
451 You can control formatting more precisely by writing @dfn{modifiers}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
452 between the @samp{%} and the character that indicates which conversion
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
453 to apply. These slightly alter the ordinary behavior of the conversion.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
454 For example, most conversion specifications permit you to specify a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
455 minimum field width and a flag indicating whether you want the result
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
456 left- or right-justified within the field.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
457
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
458 The specific flags and modifiers that are permitted and their
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
459 interpretation vary depending on the particular conversion. They're all
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
460 described in more detail in the following sections.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
461
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
462 @node Output Conversion for Matrices
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
463 @subsection Output Conversion for Matrices
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
464
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
465 When given a matrix value, Octave's formatted output functions cycle
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
466 through the format template until all the values in the matrix have been
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 9701
diff changeset
467 printed. For example:
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
468
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
469 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
470 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
471 printf ("%4.2f %10.2e %8.4g\n", hilb (3));
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
472
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
473 @print{} 1.00 5.00e-01 0.3333
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
474 @print{} 0.50 3.33e-01 0.25
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
475 @print{} 0.33 2.50e-01 0.2
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
476 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
477 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
478
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
479 If more than one value is to be printed in a single call, the output
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
480 functions do not return to the beginning of the format template when
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
481 moving on from one value to the next. This can lead to confusing output
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
482 if the number of elements in the matrices are not exact multiples of the
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 9701
diff changeset
483 number of conversions in the format template. For example:
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
484
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
485 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
486 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
487 printf ("%4.2f %10.2e %8.4g\n", [1, 2], [3, 4]);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
488
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
489 @print{} 1.00 2.00e+00 3
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
490 @print{} 4.00
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
491 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
492 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
493
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
494 If this is not what you want, use a series of calls instead of just one.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
495
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
496 @node Output Conversion Syntax
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
497 @subsection Output Conversion Syntax
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
498
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
499 This section provides details about the precise syntax of conversion
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
500 specifications that can appear in a @code{printf} template
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
501 string.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
502
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
503 Characters in the template string that are not part of a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
504 conversion specification are printed as-is to the output stream.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
505
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
506 The conversion specifications in a @code{printf} template string have
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
507 the general form:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
508
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
509 @example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
510 % @var{flags} @var{width} @r{[} . @var{precision} @r{]} @var{type} @var{conversion}
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
511 @end example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
512
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
513 For example, in the conversion specifier @samp{%-10.8ld}, the @samp{-}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
514 is a flag, @samp{10} specifies the field width, the precision is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
515 @samp{8}, the letter @samp{l} is a type modifier, and @samp{d} specifies
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
516 the conversion style. (This particular type specifier says to print a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
517 numeric argument in decimal notation, with a minimum of 8 digits
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
518 left-justified in a field at least 10 characters wide.)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
519
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
520 In more detail, output conversion specifications consist of an
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
521 initial @samp{%} character followed in sequence by:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
522
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
523 @itemize @bullet
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
524 @item
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
525 Zero or more @dfn{flag characters} that modify the normal behavior of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
526 the conversion specification.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
527 @cindex flag character (@code{printf})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
528
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
529 @item
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
530 An optional decimal integer specifying the @dfn{minimum field width}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
531 If the normal conversion produces fewer characters than this, the field
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
532 is padded with spaces to the specified width. This is a @emph{minimum}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
533 value; if the normal conversion produces more characters than this, the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
534 field is @emph{not} truncated. Normally, the output is right-justified
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
535 within the field.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
536 @cindex minimum field width (@code{printf})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
537
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
538 You can also specify a field width of @samp{*}. This means that the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
539 next argument in the argument list (before the actual value to be
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
540 printed) is used as the field width. The value is rounded to the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
541 nearest integer. If the value is negative, this means to set the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
542 @samp{-} flag (see below) and to use the absolute value as the field
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
543 width.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
544
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
545 @item
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
546 An optional @dfn{precision} to specify the number of digits to be
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
547 written for the numeric conversions. If the precision is specified, it
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
548 consists of a period (@samp{.}) followed optionally by a decimal integer
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
549 (which defaults to zero if omitted).
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
550 @cindex precision (@code{printf})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
551
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
552 You can also specify a precision of @samp{*}. This means that the next
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
553 argument in the argument list (before the actual value to be printed) is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
554 used as the precision. The value must be an integer, and is ignored
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
555 if it is negative.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
556
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
557 @item
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
558 An optional @dfn{type modifier character}. This character is ignored by
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
559 Octave's @code{printf} function, but is recognized to provide
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
560 compatibility with the C language @code{printf}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
561
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
562 @item
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
563 A character that specifies the conversion to be applied.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
564 @end itemize
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
565
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
566 The exact options that are permitted and how they are interpreted vary
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
567 between the different conversion specifiers. See the descriptions of the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
568 individual conversions for information about the particular options that
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
569 they use.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
570
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
571 @node Table of Output Conversions
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
572 @subsection Table of Output Conversions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
573 @cindex output conversions, for @code{printf}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
574
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
575 Here is a table summarizing what all the different conversions do:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
576
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
577 @table @asis
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
578 @item @samp{%d}, @samp{%i}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
579 Print an integer as a signed decimal number. @xref{Integer
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
580 Conversions}, for details. @samp{%d} and @samp{%i} are synonymous for
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
581 output, but are different when used with @code{scanf} for input
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
582 (@pxref{Table of Input Conversions}).
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
583
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
584 @item @samp{%o}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
585 Print an integer as an unsigned octal number. @xref{Integer
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
586 Conversions}, for details.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
587
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
588 @item @samp{%u}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
589 Print an integer as an unsigned decimal number. @xref{Integer
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
590 Conversions}, for details.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
591
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
592 @item @samp{%x}, @samp{%X}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
593 Print an integer as an unsigned hexadecimal number. @samp{%x} uses
14038
b0cdd60db5e5 doc: Grammarcheck documentation ahead of 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 13943
diff changeset
594 lowercase letters and @samp{%X} uses uppercase. @xref{Integer
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
595 Conversions}, for details.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
596
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
597 @item @samp{%f}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
598 Print a floating-point number in normal (fixed-point) notation.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
599 @xref{Floating-Point Conversions}, for details.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
600
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
601 @item @samp{%e}, @samp{%E}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
602 Print a floating-point number in exponential notation. @samp{%e} uses
14038
b0cdd60db5e5 doc: Grammarcheck documentation ahead of 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 13943
diff changeset
603 lowercase letters and @samp{%E} uses uppercase. @xref{Floating-Point
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
604 Conversions}, for details.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
605
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
606 @item @samp{%g}, @samp{%G}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
607 Print a floating-point number in either normal (fixed-point) or
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
608 exponential notation, whichever is more appropriate for its magnitude.
14038
b0cdd60db5e5 doc: Grammarcheck documentation ahead of 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 13943
diff changeset
609 @samp{%g} uses lowercase letters and @samp{%G} uses uppercase.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
610 @xref{Floating-Point Conversions}, for details.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
611
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
612 @item @samp{%c}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
613 Print a single character. @xref{Other Output Conversions}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
614
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
615 @item @samp{%s}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
616 Print a string. @xref{Other Output Conversions}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
617
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
618 @item @samp{%%}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
619 Print a literal @samp{%} character. @xref{Other Output Conversions}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
620 @end table
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
621
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
622 If the syntax of a conversion specification is invalid, unpredictable
19476
8ee14c64ab5f Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents: 19312
diff changeset
623 things will happen, so don't do this. In particular, @sc{matlab} allows
8ee14c64ab5f Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents: 19312
diff changeset
624 a bare percentage sign @samp{%} with no subsequent conversion character.
8ee14c64ab5f Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents: 19312
diff changeset
625 Octave will emit an error and stop if it sees such code. When the string
8ee14c64ab5f Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents: 19312
diff changeset
626 variable to be processed cannot be guaranteed to be free of potential format
8ee14c64ab5f Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents: 19312
diff changeset
627 codes it is better to use the two argument form of any of the @code{printf}
19486
05eb8eaf63d3 doc: Add puts as a safe alternative to display a string with bare '%' chars (bug #42345).
Rik <rik@octave.org>
parents: 19476
diff changeset
628 functions and set the format string to @code{%s}. Alternatively, for code
05eb8eaf63d3 doc: Add puts as a safe alternative to display a string with bare '%' chars (bug #42345).
Rik <rik@octave.org>
parents: 19476
diff changeset
629 which is not required to be backwards-compatible with @sc{matlab} the
05eb8eaf63d3 doc: Add puts as a safe alternative to display a string with bare '%' chars (bug #42345).
Rik <rik@octave.org>
parents: 19476
diff changeset
630 Octave function @code{puts} or @code{disp} can be used.
19476
8ee14c64ab5f Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents: 19312
diff changeset
631
8ee14c64ab5f Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents: 19312
diff changeset
632 @example
8ee14c64ab5f Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents: 19312
diff changeset
633 @group
8ee14c64ab5f Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents: 19312
diff changeset
634 printf (strvar); # Unsafe if strvar contains format codes
8ee14c64ab5f Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents: 19312
diff changeset
635 printf ("%s", strvar); # Safe
19486
05eb8eaf63d3 doc: Add puts as a safe alternative to display a string with bare '%' chars (bug #42345).
Rik <rik@octave.org>
parents: 19476
diff changeset
636 puts (strvar); # Safe
19476
8ee14c64ab5f Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents: 19312
diff changeset
637 @end group
8ee14c64ab5f Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents: 19312
diff changeset
638 @end example
8ee14c64ab5f Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents: 19312
diff changeset
639
8ee14c64ab5f Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents: 19312
diff changeset
640 If there aren't enough function arguments provided to supply values for all
8ee14c64ab5f Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents: 19312
diff changeset
641 the conversion specifications in the template string, or if the arguments are
8ee14c64ab5f Document Matlab incompatibility in printf functions and bare '%' char (bug #42345).
Rik <rik@octave.org>
parents: 19312
diff changeset
642 not of the correct types, the results are unpredictable. If you supply more
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
643 arguments than conversion specifications, the extra argument values are
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
644 simply ignored; this is sometimes useful.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
645
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
646 @node Integer Conversions
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
647 @subsection Integer Conversions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
648
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
649 This section describes the options for the @samp{%d}, @samp{%i},
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
650 @samp{%o}, @samp{%u}, @samp{%x}, and @samp{%X} conversion
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
651 specifications. These conversions print integers in various formats.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
652
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
653 The @samp{%d} and @samp{%i} conversion specifications both print an
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
654 numeric argument as a signed decimal number; while @samp{%o},
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
655 @samp{%u}, and @samp{%x} print the argument as an unsigned octal,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
656 decimal, or hexadecimal number (respectively). The @samp{%X} conversion
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
657 specification is just like @samp{%x} except that it uses the characters
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
658 @samp{ABCDEF} as digits instead of @samp{abcdef}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
659
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
660 The following flags are meaningful:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
661
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
662 @table @asis
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
663 @item @samp{-}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
664 Left-justify the result in the field (instead of the normal
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
665 right-justification).
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
666
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
667 @item @samp{+}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
668 For the signed @samp{%d} and @samp{%i} conversions, print a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
669 plus sign if the value is positive.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
670
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
671 @item @samp{ }
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
672 For the signed @samp{%d} and @samp{%i} conversions, if the result
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
673 doesn't start with a plus or minus sign, prefix it with a space
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
674 character instead. Since the @samp{+} flag ensures that the result
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
675 includes a sign, this flag is ignored if you supply both of them.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
676
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
677 @item @samp{#}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
678 For the @samp{%o} conversion, this forces the leading digit to be
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
679 @samp{0}, as if by increasing the precision. For @samp{%x} or
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
680 @samp{%X}, this prefixes a leading @samp{0x} or @samp{0X} (respectively)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
681 to the result. This doesn't do anything useful for the @samp{%d},
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
682 @samp{%i}, or @samp{%u} conversions.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
683
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
684 @item @samp{0}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
685 Pad the field with zeros instead of spaces. The zeros are placed after
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
686 any indication of sign or base. This flag is ignored if the @samp{-}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
687 flag is also specified, or if a precision is specified.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
688 @end table
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
689
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
690 If a precision is supplied, it specifies the minimum number of digits to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
691 appear; leading zeros are produced if necessary. If you don't specify a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
692 precision, the number is printed with as many digits as it needs. If
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
693 you convert a value of zero with an explicit precision of zero, then no
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
694 characters at all are produced.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
695
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
696 @node Floating-Point Conversions
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
697 @subsection Floating-Point Conversions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
698
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
699 This section discusses the conversion specifications for floating-point
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
700 numbers: the @samp{%f}, @samp{%e}, @samp{%E}, @samp{%g}, and @samp{%G}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
701 conversions.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
702
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
703 The @samp{%f} conversion prints its argument in fixed-point notation,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
704 producing output of the form
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
705 @w{[@code{-}]@var{ddd}@code{.}@var{ddd}},
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
706 where the number of digits following the decimal point is controlled
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
707 by the precision you specify.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
708
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
709 The @samp{%e} conversion prints its argument in exponential notation,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
710 producing output of the form
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
711 @w{[@code{-}]@var{d}@code{.}@var{ddd}@code{e}[@code{+}|@code{-}]@var{dd}}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
712 Again, the number of digits following the decimal point is controlled by
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
713 the precision. The exponent always contains at least two digits. The
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
714 @samp{%E} conversion is similar but the exponent is marked with the letter
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
715 @samp{E} instead of @samp{e}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
716
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
717 The @samp{%g} and @samp{%G} conversions print the argument in the style
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
718 of @samp{%e} or @samp{%E} (respectively) if the exponent would be less
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
719 than -4 or greater than or equal to the precision; otherwise they use the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
720 @samp{%f} style. Trailing zeros are removed from the fractional portion
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
721 of the result and a decimal-point character appears only if it is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
722 followed by a digit.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
723
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
724 The following flags can be used to modify the behavior:
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17152
diff changeset
725 @c Not @samp so we can have ' ' as an item.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
726
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
727 @table @asis
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
728 @item @samp{-}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
729 Left-justify the result in the field. Normally the result is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
730 right-justified.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
731
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
732 @item @samp{+}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
733 Always include a plus or minus sign in the result.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
734
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
735 @item @samp{ }
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
736 If the result doesn't start with a plus or minus sign, prefix it with a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
737 space instead. Since the @samp{+} flag ensures that the result includes
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
738 a sign, this flag is ignored if you supply both of them.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
739
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
740 @item @samp{#}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
741 Specifies that the result should always include a decimal point, even
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
742 if no digits follow it. For the @samp{%g} and @samp{%G} conversions,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
743 this also forces trailing zeros after the decimal point to be left
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
744 in place where they would otherwise be removed.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
745
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
746 @item @samp{0}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
747 Pad the field with zeros instead of spaces; the zeros are placed
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
748 after any sign. This flag is ignored if the @samp{-} flag is also
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
749 specified.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
750 @end table
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
751
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
752 The precision specifies how many digits follow the decimal-point
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
753 character for the @samp{%f}, @samp{%e}, and @samp{%E} conversions. For
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
754 these conversions, the default precision is @code{6}. If the precision
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
755 is explicitly @code{0}, this suppresses the decimal point character
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
756 entirely. For the @samp{%g} and @samp{%G} conversions, the precision
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
757 specifies how many significant digits to print. Significant digits are
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
758 the first digit before the decimal point, and all the digits after it.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
759 If the precision is @code{0} or not specified for @samp{%g} or
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
760 @samp{%G}, it is treated like a value of @code{1}. If the value being
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
761 printed cannot be expressed precisely in the specified number of digits,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
762 the value is rounded to the nearest number that fits.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
763
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
764 @node Other Output Conversions
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
765 @subsection Other Output Conversions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
766
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
767 This section describes miscellaneous conversions for @code{printf}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
768
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
769 The @samp{%c} conversion prints a single character. The @samp{-}
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
770 flag can be used to specify left-justification in the field, but no
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
771 other flags are defined, and no precision or type modifier can be given.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
772 For example:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
773
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
774 @example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
775 printf ("%c%c%c%c%c", "h", "e", "l", "l", "o");
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
776 @end example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
777
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
778 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
779 prints @samp{hello}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
780
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
781 The @samp{%s} conversion prints a string. The corresponding argument
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
782 must be a string. A precision can be specified to indicate the maximum
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
783 number of characters to write; otherwise characters in the string up to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
784 but not including the terminating null character are written to the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
785 output stream. The @samp{-} flag can be used to specify
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
786 left-justification in the field, but no other flags or type modifiers
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
787 are defined for this conversion. For example:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
788
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
789 @example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
790 printf ("%3s%-6s", "no", "where");
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
791 @end example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
792
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
793 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
794 prints @samp{ nowhere } (note the leading and trailing spaces).
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
795
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
796 @node Formatted Input
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
797 @subsection Formatted Input
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
798
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
799 Octave provides the @code{scanf}, @code{fscanf}, and @code{sscanf}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
800 functions to read formatted input. There are two forms of each of these
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
801 functions. One can be used to extract vectors of data from a file, and
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
802 the other is more `C-like'.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
803
3428
5b77cf82393c [project @ 2000-01-14 02:54:53 by jwe]
jwe
parents: 3373
diff changeset
804 @DOCSTRING(fscanf)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
805
8286
6f2d95255911 fix @seealso references to point to existing anchors
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7580
diff changeset
806 @DOCSTRING(scanf)
6f2d95255911 fix @seealso references to point to existing anchors
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7580
diff changeset
807
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
808 @DOCSTRING(sscanf)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
809
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
810 Calls to @code{scanf} are superficially similar to calls to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
811 @code{printf} in that arbitrary arguments are read under the control of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
812 a template string. While the syntax of the conversion specifications in
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
813 the template is very similar to that for @code{printf}, the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
814 interpretation of the template is oriented more towards free-format
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
815 input and simple pattern matching, rather than fixed-field formatting.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
816 For example, most @code{scanf} conversions skip over any amount of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
817 ``white space'' (including spaces, tabs, and newlines) in the input
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
818 file, and there is no concept of precision for the numeric input
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
819 conversions as there is for the corresponding output conversions.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
820 Ordinarily, non-whitespace characters in the template are expected to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
821 match characters in the input stream exactly.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
822 @cindex conversion specifications (@code{scanf})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
823
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
824 When a @dfn{matching failure} occurs, @code{scanf} returns immediately,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
825 leaving the first non-matching character as the next character to be
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
826 read from the stream, and @code{scanf} returns all the items that were
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
827 successfully converted.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
828 @cindex matching failure, in @code{scanf}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
829
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
830 The formatted input functions are not used as frequently as the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
831 formatted output functions. Partly, this is because it takes some care
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
832 to use them properly. Another reason is that it is difficult to recover
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
833 from a matching error.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
834
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
835 @node Input Conversion Syntax
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
836 @subsection Input Conversion Syntax
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
837
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
838 A @code{scanf} template string is a string that contains ordinary
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
839 multibyte characters interspersed with conversion specifications that
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
840 start with @samp{%}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
841
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
842 Any whitespace character in the template causes any number of whitespace
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
843 characters in the input stream to be read and discarded. The whitespace
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
844 characters that are matched need not be exactly the same whitespace
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
845 characters that appear in the template string. For example, write
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
846 @samp{ , } in the template to recognize a comma with optional whitespace
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
847 before and after.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
848
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
849 Other characters in the template string that are not part of conversion
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
850 specifications must match characters in the input stream exactly; if
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
851 this is not the case, a matching failure occurs.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
852
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
853 The conversion specifications in a @code{scanf} template string
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
854 have the general form:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
855
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
856 @example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
857 % @var{flags} @var{width} @var{type} @var{conversion}
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
858 @end example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
859
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
860 In more detail, an input conversion specification consists of an initial
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
861 @samp{%} character followed in sequence by:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
862
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
863 @itemize @bullet
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
864 @item
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
865 An optional @dfn{flag character} @samp{*}, which says to ignore the text
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
866 read for this specification. When @code{scanf} finds a conversion
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
867 specification that uses this flag, it reads input as directed by the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
868 rest of the conversion specification, but it discards this input, does
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
869 not return any value, and does not increment the count of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
870 successful assignments.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
871 @cindex flag character (@code{scanf})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
872
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
873 @item
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
874 An optional decimal integer that specifies the @dfn{maximum field
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
875 width}. Reading of characters from the input stream stops either when
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
876 this maximum is reached or when a non-matching character is found,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
877 whichever happens first. Most conversions discard initial whitespace
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
878 characters, and these discarded characters don't count towards the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
879 maximum field width. Conversions that do not discard initial whitespace
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
880 are explicitly documented.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
881 @cindex maximum field width (@code{scanf})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
882
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
883 @item
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
884 An optional type modifier character. This character is ignored by
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
885 Octave's @code{scanf} function, but is recognized to provide
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
886 compatibility with the C language @code{scanf}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
887
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
888 @item
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
889 A character that specifies the conversion to be applied.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
890 @end itemize
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
891
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
892 The exact options that are permitted and how they are interpreted vary
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
893 between the different conversion specifiers. See the descriptions of the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
894 individual conversions for information about the particular options that
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
895 they allow.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
896
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
897 @node Table of Input Conversions
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
898 @subsection Table of Input Conversions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
899 @cindex input conversions, for @code{scanf}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
900
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
901 Here is a table that summarizes the various conversion specifications:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
902
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
903 @table @asis
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
904 @item @samp{%d}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
905 Matches an optionally signed integer written in decimal. @xref{Numeric
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
906 Input Conversions}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
907
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
908 @item @samp{%i}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
909 Matches an optionally signed integer in any of the formats that the C
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
910 language defines for specifying an integer constant. @xref{Numeric
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
911 Input Conversions}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
912
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
913 @item @samp{%o}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
914 Matches an unsigned integer written in octal radix.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
915 @xref{Numeric Input Conversions}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
916
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
917 @item @samp{%u}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
918 Matches an unsigned integer written in decimal radix.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
919 @xref{Numeric Input Conversions}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
920
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
921 @item @samp{%x}, @samp{%X}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
922 Matches an unsigned integer written in hexadecimal radix.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
923 @xref{Numeric Input Conversions}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
924
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
925 @item @samp{%e}, @samp{%f}, @samp{%g}, @samp{%E}, @samp{%G}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
926 Matches an optionally signed floating-point number. @xref{Numeric Input
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
927 Conversions}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
928
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
929 @item @samp{%s}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
930 Matches a string containing only non-whitespace characters.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
931 @xref{String Input Conversions}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
932
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
933 @item @samp{%c}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
934 Matches a string of one or more characters; the number of characters
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
935 read is controlled by the maximum field width given for the conversion.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
936 @xref{String Input Conversions}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
937
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
938 @item @samp{%%}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
939 This matches a literal @samp{%} character in the input stream. No
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
940 corresponding argument is used.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
941 @end table
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
942
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
943 If the syntax of a conversion specification is invalid, the behavior is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
944 undefined. If there aren't enough function arguments provided to supply
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
945 addresses for all the conversion specifications in the template strings
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
946 that perform assignments, or if the arguments are not of the correct
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
947 types, the behavior is also undefined. On the other hand, extra
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
948 arguments are simply ignored.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
949
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
950 @node Numeric Input Conversions
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
951 @subsection Numeric Input Conversions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
952
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
953 This section describes the @code{scanf} conversions for reading numeric
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
954 values.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
955
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
956 The @samp{%d} conversion matches an optionally signed integer in decimal
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
957 radix.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
958
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
959 The @samp{%i} conversion matches an optionally signed integer in any of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
960 the formats that the C language defines for specifying an integer
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
961 constant.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
962
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
963 For example, any of the strings @samp{10}, @samp{0xa}, or @samp{012}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
964 could be read in as integers under the @samp{%i} conversion. Each of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
965 these specifies a number with decimal value @code{10}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
966
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
967 The @samp{%o}, @samp{%u}, and @samp{%x} conversions match unsigned
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
968 integers in octal, decimal, and hexadecimal radices, respectively.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
969
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
970 The @samp{%X} conversion is identical to the @samp{%x} conversion. They
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
971 both permit either uppercase or lowercase letters to be used as digits.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
972
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
973 Unlike the C language @code{scanf}, Octave ignores the @samp{h},
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
974 @samp{l}, and @samp{L} modifiers.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
975
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
976 @node String Input Conversions
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
977 @subsection String Input Conversions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
978
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
979 This section describes the @code{scanf} input conversions for reading
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
980 string and character values: @samp{%s} and @samp{%c}.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
981
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
982 The @samp{%c} conversion is the simplest: it matches a fixed number of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
983 characters, always. The maximum field with says how many characters to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
984 read; if you don't specify the maximum, the default is 1. This
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
985 conversion does not skip over initial whitespace characters. It reads
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
986 precisely the next @var{n} characters, and fails if it cannot get that
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
987 many.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
988
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
989 The @samp{%s} conversion matches a string of non-whitespace characters.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
990 It skips and discards initial whitespace, but stops when it encounters
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
991 more whitespace after having read something.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
992
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
993 For example, reading the input:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
994
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
995 @example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
996 hello, world
6670
14992092ab06 [project @ 2007-05-29 17:51:37 by jwe]
jwe
parents: 6666
diff changeset
997 @end example
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
998
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
999 @noindent
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17152
diff changeset
1000 with the conversion @samp{%10c} produces @qcode{" hello, wo"}, but
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1001 reading the same input with the conversion @samp{%10s} produces
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17152
diff changeset
1002 @qcode{"hello,"}.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1003
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
1004 @node Binary I/O
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1005 @subsection Binary I/O
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1006
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1007 Octave can read and write binary data using the functions @code{fread}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1008 and @code{fwrite}, which are patterned after the standard C functions
6939
46d1ad37d943 [project @ 2007-10-01 16:12:20 by jwe]
jwe
parents: 6788
diff changeset
1009 with the same names. They are able to automatically swap the byte order
46d1ad37d943 [project @ 2007-10-01 16:12:20 by jwe]
jwe
parents: 6788
diff changeset
1010 of integer data and convert among the supported floating point formats
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1011 as the data are read.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1012
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
1013 @DOCSTRING(fread)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1014
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
1015 @DOCSTRING(fwrite)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1016
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
1017 @node Temporary Files
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1018 @subsection Temporary Files
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1019
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1020 Sometimes one needs to write data to a file that is only temporary.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1021 This is most commonly used when an external program launched from
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1022 within Octave needs to access data. When Octave exits all temporary
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1023 files will be deleted, so this step need not be executed manually.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1024
4328
f7b63f362168 [project @ 2003-02-16 04:29:00 by jwe]
jwe
parents: 4169
diff changeset
1025 @DOCSTRING(mkstemp)
f7b63f362168 [project @ 2003-02-16 04:29:00 by jwe]
jwe
parents: 4169
diff changeset
1026
f7b63f362168 [project @ 2003-02-16 04:29:00 by jwe]
jwe
parents: 4169
diff changeset
1027 @DOCSTRING(tmpfile)
f7b63f362168 [project @ 2003-02-16 04:29:00 by jwe]
jwe
parents: 4169
diff changeset
1028
19312
6ca096827123 Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents: 17744
diff changeset
1029 @DOCSTRING(tempname)
6ca096827123 Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents: 17744
diff changeset
1030
6ca096827123 Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents: 17744
diff changeset
1031 @DOCSTRING(tempdir)
6ca096827123 Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents: 17744
diff changeset
1032
6ca096827123 Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents: 17744
diff changeset
1033 @DOCSTRING(P_tmpdir)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1034
17152
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16875
diff changeset
1035 @node EOF and Errors
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1036 @subsection End of File and Errors
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1037
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1038 Once a file has been opened its status can be acquired. As an example
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1039 the @code{feof} functions determines if the end of the file has been
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1040 reached. This can be very useful when reading small parts of a file
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1041 at a time. The following example shows how to read one line at a time
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1042 from a file until the end has been reached.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1043
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1044 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9039
diff changeset
1045 @group
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1046 filename = "myfile.txt";
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1047 fid = fopen (filename, "r");
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1048 while (! feof (fid) )
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1049 text_line = fgetl (fid);
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1050 endwhile
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1051 fclose (fid);
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9039
diff changeset
1052 @end group
6666
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1053 @end example
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1054
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1055 @noindent
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1056 Note that in some situations it is more efficient to read the entire
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1057 contents of a file and then process it, than it is to read it line by
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1058 line. This has the potential advantage of removing the loop in the
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1059 above code.
5d4ce539004f [project @ 2007-05-28 06:07:39 by jwe]
jwe
parents: 6646
diff changeset
1060
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
1061 @DOCSTRING(feof)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1062
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
1063 @DOCSTRING(ferror)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1064
8817
03b7f618ab3d include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents: 8569
diff changeset
1065 @DOCSTRING(fclear)
03b7f618ab3d include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents: 8569
diff changeset
1066
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
1067 @DOCSTRING(freport)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1068
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3709
diff changeset
1069 @node File Positioning
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1070 @subsection File Positioning
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1071
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1072 Three functions are available for setting and determining the position of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1073 the file pointer for a given file.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1074
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
1075 @DOCSTRING(ftell)
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
1076
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
1077 @DOCSTRING(fseek)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1078
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
1079 @DOCSTRING(SEEK_SET)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1080
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3294
diff changeset
1081 @DOCSTRING(frewind)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1082
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1083 The following example stores the current file position in the variable
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1084 @code{marker}, moves the pointer to the beginning of the file, reads
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1085 four characters, and then returns to the original position.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1086
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1087 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9039
diff changeset
1088 @group
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1089 marker = ftell (myfile);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1090 frewind (myfile);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1091 fourch = fgets (myfile, 4);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1092 fseek (myfile, marker, SEEK_SET);
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9039
diff changeset
1093 @end group
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1094 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1095