comparison doc/interpreter/io.texi @ 2689:8c7955a8d49f

[project @ 1997-02-18 09:06:10 by jwe]
author jwe
date Tue, 18 Feb 1997 09:09:12 +0000
parents 18192eea4973
children 99dd10f4eaaf
comparison
equal deleted inserted replaced
2688:fe5e83216458 2689:8c7955a8d49f
98 has been evaluated, the simplest of all I/O functions is a simple 98 has been evaluated, the simplest of all I/O functions is a simple
99 expression. For example, the following expression will display the 99 expression. For example, the following expression will display the
100 value of pi 100 value of pi
101 101
102 @example 102 @example
103 octave:13> pi 103 pi
104 pi = 3.1416 104 @print{} pi = 3.1416
105 @end example 105 @end example
106 106
107 This works well as long as it is acceptable to have the name of the 107 This works well as long as it is acceptable to have the name of the
108 variable (or @samp{ans}) printed along with the value. To print the 108 variable (or @samp{ans}) printed along with the value. To print the
109 value of a variable without printing its name, use the function 109 value of a variable without printing its name, use the function
119 @example 119 @example
120 3^2 + 4^2 120 3^2 + 4^2
121 @end example 121 @end example
122 122
123 @noindent 123 @noindent
124 is evaluated, the value of @code{ans} is @samp{25}. 124 is evaluated, the value of @code{ans} is 25.
125 @end defvr 125 @end defvr
126 126
127 @deftypefn {Built-in Function} {} disp (@var{x}) 127 @deftypefn {Built-in Function} {} disp (@var{x})
128 Display the value of @var{x}. For example, the following expression 128 Display the value of @var{x}. For example,
129 129
130 @example 130 @example
131 disp ("The value of pi is:"), disp (pi) 131 disp ("The value of pi is:"), disp (pi)
132 @end example 132
133 133 @print{} the value of pi is:
134 @noindent 134 @print{} 3.1416
135 will print
136
137 @example
138 The value of pi is:
139 3.1416
140 @end example 135 @end example
141 136
142 @noindent 137 @noindent
143 Note that the output from @code{disp} always ends with a newline. 138 Note that the output from @code{disp} always ends with a newline.
144 @end deftypefn 139 @end deftypefn
168 163
169 @item long e 164 @item long e
170 @itemx short e 165 @itemx short e
171 The same as @samp{format long} or @samp{format short} but always display 166 The same as @samp{format long} or @samp{format short} but always display
172 output with an @samp{e} format. For example, with the @samp{short e} 167 output with an @samp{e} format. For example, with the @samp{short e}
173 format, pi is displayed as 168 format, pi is displayed as @code{3.14e+00}.
174
175 @example
176 3.14e+00
177 @end example
178 169
179 @item long E 170 @item long E
180 @itemx short E 171 @itemx short E
181 The same as @samp{format long e} or @samp{format short e} but always 172 The same as @samp{format long e} or @samp{format short e} but always
182 display output with an uppercase @samp{E} format. For example, with 173 display output with an uppercase @samp{E} format. For example, with
183 the @samp{long E} format, pi is displayed as 174 the @samp{long E} format, pi is displayed as
184 175 @code{3.14159265358979E+00}.
185 @example
186 3.14159265358979E+00
187 @end example
188 176
189 @item free 177 @item free
190 @itemx none 178 @itemx none
191 Print output in free format, without trying to line up columns of 179 Print output in free format, without trying to line up columns of
192 matrices on the decimal point. This also causes complex numbers to be 180 matrices on the decimal point. This also causes complex numbers to be
373 character is @code{!} or @code{^}, match all characters except those 361 character is @code{!} or @code{^}, match all characters except those
374 specified by @var{list}. For example, the pattern @samp{[a-zA-Z]} will 362 specified by @var{list}. For example, the pattern @samp{[a-zA-Z]} will
375 match all lower and upper case alphabetic characters. 363 match all lower and upper case alphabetic characters.
376 @end table 364 @end table
377 365
378 Except when using hte @sc{Matlab} binary data file format, saving global 366 Except when using the @sc{Matlab} binary data file format, saving global
379 variables also saves the global status of the variable, so that if it is 367 variables also saves the global status of the variable, so that if it is
380 restored at a later time using @samp{load}, it will be restored as a 368 restored at a later time using @samp{load}, it will be restored as a
381 global variable. 369 global variable.
382 370
383 The command 371 The command
404 This variable specifies the number of digits to keep when saving data in 392 This variable specifies the number of digits to keep when saving data in
405 text format. The default value is 17. 393 text format. The default value is 17.
406 @end defvr 394 @end defvr
407 395
408 @deffn {Command} load options file v1 v2 @dots{} 396 @deffn {Command} load options file v1 v2 @dots{}
409 To restore the values from a file, use the @code{load} command. As with 397 Load the named variables from the file @var{file}. As with @code{save},
410 @code{save}, you may specify a list of variables and @code{load} will 398 you may specify a list of variables and @code{load} will only extract
411 only extract those variables with names that match. For example, to 399 those variables with names that match. For example, to restore the
412 restore the variables saved in the file @file{data}, use the command 400 variables saved in the file @file{data}, use the command
413 401
414 @example 402 @example
415 load data 403 load data
416 @end example 404 @end example
417 405
456 Octave's C-style input and output functions provide most of the 444 Octave's C-style input and output functions provide most of the
457 functionality of the C programming language's standard I/O library. The 445 functionality of the C programming language's standard I/O library. The
458 argument lists for some of the input functions are slightly different, 446 argument lists for some of the input functions are slightly different,
459 however, because Octave has no way of passing arguments by reference. 447 however, because Octave has no way of passing arguments by reference.
460 448
461 In the following, @var{file} refers to a file name and @code{fid} returs 449 In the following, @var{file} refers to a file name and @code{fid} refers
462 to an integer file number, as returned by @code{fopen}. 450 to an integer file number, as returned by @code{fopen}.
463 451
464 There are three files that are always available. Although these files 452 There are three files that are always available. Although these files
465 can be accessed using their corresponding numeric file ids, you should 453 can be accessed using their corresponding numeric file ids, you should
466 always use the symbolic names given in the table below, since it will 454 always use the symbolic names given in the table below, since it will
506 @end menu 494 @end menu
507 495
508 @node Opening and Closing Files, Simple Output, C-Style I/O Functions, C-Style I/O Functions 496 @node Opening and Closing Files, Simple Output, C-Style I/O Functions, C-Style I/O Functions
509 @subsection Opening and Closing Files 497 @subsection Opening and Closing Files
510 498
511 @deftypefn {Built-in Function} {[fid, msg] =} fopen (@var{name}, @var{mode}, @var{arch}) 499 @deftypefn {Built-in Function} {[@var{fid}, @var{msg}] =} fopen (@var{name}, @var{mode}, @var{arch})
512 @deftypefnx {Built-in Function} {fid_list =} fopen ("all") 500 @deftypefnx {Built-in Function} {@var{fid_list} =} fopen ("all")
513 @deftypefnx {Built-in Function} {file =} fopen (@var{fid}) 501 @deftypefnx {Built-in Function} {@var{file} =} fopen (@var{fid})
514 The first form of the @code{fopen} function opens the named file with 502 The first form of the @code{fopen} function opens the named file with
515 the specified mode (read-write, read-only, etc.) and architecture 503 the specified mode (read-write, read-only, etc.) and architecture
516 interpretation (IEEE big endian, IEEE little endian, etc.), and returns 504 interpretation (IEEE big endian, IEEE little endian, etc.), and returns
517 an integer value that may be used to refer to the file later. If an 505 an integer value that may be used to refer to the file later. If an
518 error occurs, @var{fid} is set to -1 and @var{msg} contains the 506 error occurs, @var{fid} is set to @minus{}1 and @var{msg} contains the
519 corresponding system error message. The @var{mode} is a one or two 507 corresponding system error message. The @var{mode} is a one or two
520 character string that specifies whether the file is to be opened for 508 character string that specifies whether the file is to be opened for
521 reading, writing, or both. 509 reading, writing, or both.
522 510
523 The second form of the @code{fopen} function returns a vector of file ids 511 The second form of the @code{fopen} function returns a vector of file ids
623 the returned value. 611 the returned value.
624 612
625 If @var{len} is omitted, @code{fgetl} reads until the next newline 613 If @var{len} is omitted, @code{fgetl} reads until the next newline
626 character. 614 character.
627 615
628 If there are no more characters to read, @code{fgetl} returns -1. 616 If there are no more characters to read, @code{fgetl} returns @minus{}1.
629 @end deftypefn 617 @end deftypefn
630 618
631 @deftypefn {Built-in Function} {} fgets (@var{fid}, @var{len}) 619 @deftypefn {Built-in Function} {} fgets (@var{fid}, @var{len})
632 Read characters from a file, stopping at the first newline character 620 Read characters from a file, stopping at the first newline character
633 that is encountered or after @var{len} characters have been read, and 621 that is encountered or after @var{len} characters have been read, and
635 returned value. 623 returned value.
636 624
637 If @var{len} is omitted, @code{fgets} reads until the next newline 625 If @var{len} is omitted, @code{fgets} reads until the next newline
638 character. 626 character.
639 627
640 If there are no more characters to read, @code{fgets} returns -1. 628 If there are no more characters to read, @code{fgets} returns @minus{}1.
641 @end deftypefn 629 @end deftypefn
642 630
643 @node Formatted Output, Output Conversion for Matrices, Line-Oriented Input, C-Style I/O Functions 631 @node Formatted Output, Output Conversion for Matrices, Line-Oriented Input, C-Style I/O Functions
644 @subsection Formatted Output 632 @subsection Formatted Output
645 633
1041 prints @samp{ nowhere } (note the leading and trailing spaces). 1029 prints @samp{ nowhere } (note the leading and trailing spaces).
1042 1030
1043 @node Formatted Input, Input Conversion Syntax, Other Output Conversions, C-Style I/O Functions 1031 @node Formatted Input, Input Conversion Syntax, Other Output Conversions, C-Style I/O Functions
1044 @subsection Formatted Input 1032 @subsection Formatted Input
1045 1033
1046 Here are the descriptions of the functions for performing formatted 1034 Octave provides the @code{scanf}, @code{fscanf}, and @code{sscanf}
1047 input. 1035 functions to read formatted input. There are two forms of each of these
1048 1036 functions. One can be used to extract vectors of data from a file, and
1049 @deftypefn {Built-in Function} {} scanf (@var{template}) 1037 the other is more `C-like'.
1050 @deftypefnx {Built-in Function} {} scanf (@var{template}, "C") 1038
1051 The @code{scanf} function reads formatted input from the stream 1039 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} fscanf (@var{fid}, @var{template}, @var{size})
1052 @code{stdin} under the control of the template string @var{template}. 1040 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}] = } fscanf (@var{fid}, @var{template}, "C")
1053 The resulting values are returned. 1041 In the first form, read from @var{fid} according to @var{template},
1054 @end deftypefn 1042 returning the result in the matrix @var{val}.
1055 1043
1056 @deftypefn {Built-in Function} {} fscanf (@var{fid}, @var{template}) 1044 The optional argument @var{size} specifies the amount of data to read
1057 @deftypefnx {Built-in Function} {} fscanf (@var{fid}, @var{template}, "C") 1045 and may be one of
1058 This function is just like @code{scanf}, except that the input is read 1046
1059 from the stream @var{fid} instead of @code{stdin}. 1047 @table @code
1060 @end deftypefn 1048 @item Inf
1061 1049 Read as much as possible, returning a column vector.
1062 @deftypefn {Built-in Function} {} sscanf (@var{string}, @var{template}) 1050
1063 @deftypefnx {Built-in Function} {} sscanf (@var{string}, @var{template}, "C") 1051 @item @var{nr}
1064 This is like @code{scanf}, except that the characters are taken from the 1052 @itemx [@var{nr}, Inf]
1053 Read as much as possible, returning a matrix with @var{nr} rows. If the
1054 number of elements read is not an exact multiple of @var{nr}, the last
1055 column is padded with zeros.
1056
1057 @item [@var{nr}, @var{nc}]
1058 Read up to @code{@var{nr} * @var{nc}} elements, returning a matrix with
1059 @var{nr} rows. If the number of elements read is not an exact multiple
1060 of @var{nr}, the last column is padded with zeros.
1061 @end table
1062
1063 @noindent
1064 If @var{size} is omitted, a value of @code{Inf} is assumed.
1065
1066 A string is returned if @var{template} specifies only character
1067 conversions.
1068
1069 The number of items successfully read is returned in @var{count}.
1070
1071 In the second form, read from @var{fid} according to @var{template},
1072 with each conversion specifier in @var{template} corresponding to a
1073 single scalar return value. This form is more `C-like', and also
1074 compatible with previous versions of Octave.
1075 @end deftypefn
1076
1077 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} sscanf (@var{string}, @var{template}, @var{size})
1078 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}] = } sscanf (@var{string}, @var{template}, "C")
1079 This is like @code{fscanf}, except that the characters are taken from the
1065 string @var{string} instead of from a stream. Reaching the end of the 1080 string @var{string} instead of from a stream. Reaching the end of the
1066 string is treated as an end-of-file condition. 1081 string is treated as an end-of-file condition.
1082 @end deftypefn
1083
1084 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} scanf (@var{template}, @var{size})
1085 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}] = } scanf (@var{template}, "C")
1086 This is equivalent to calling @code{fscanf} with @var{fid} = @code{stdin}.
1087
1088 It is currently not useful to call @code{scanf} in interactive
1089 programs.
1067 @end deftypefn 1090 @end deftypefn
1068 1091
1069 Calls to @code{scanf} are superficially similar to calls to 1092 Calls to @code{scanf} are superficially similar to calls to
1070 @code{printf} in that arbitrary arguments are read under the control of 1093 @code{printf} in that arbitrary arguments are read under the control of
1071 a template string. While the syntax of the conversion specifications in 1094 a template string. While the syntax of the conversion specifications in
1260 @code{"hello,"}. 1283 @code{"hello,"}.
1261 1284
1262 @node Binary I/O, Temporary Files, String Input Conversions, C-Style I/O Functions 1285 @node Binary I/O, Temporary Files, String Input Conversions, C-Style I/O Functions
1263 @subsection Binary I/O 1286 @subsection Binary I/O
1264 1287
1265 Octave has to C-style functions for reading and writing binary data. 1288 Octave can read and write binary data using the functions @code{fread}
1266 They are @code{fread} and @code{fwrite} and are patterned after the 1289 and @code{fwrite}, which are patterned after the standard C functions
1267 standard C functions with the same names. 1290 with the same names. The are able to automatically swap the byte order
1268 1291 of integer data and convert among ths supported floating point formats
1269 @deftypefn {Built-in Function} {} fread (@var{fid}, @var{size}, @var{precision}, @var{arch}) 1292 as the data are read.
1270 This function reads data in binary form of type @var{precision} from the 1293
1271 specified @var{fid}, which may be either a file name, or a file id 1294 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} fread (@var{fid}, @var{size}, @var{precision}, @var{skip}, @var{arch})
1272 as returned from @code{fopen}. 1295 Read binary data of type @var{precision} from the specified file ID
1273 1296 @var{fid}.
1274 The argument @var{size} specifies the size of the matrix to return. It 1297
1275 may be a scalar or a two-element vector. If it is a scalar, 1298 The optional argument @var{size} specifies the amount of data to read
1276 @code{fread} returns a column vector of the specified length. If it is 1299 and may be one of
1277 a two-element vector, it specifies the number of rows and columns of the 1300
1278 result matrix, and @code{fread} fills the elements of the matrix in 1301 @table @code
1279 column-major order. 1302 @item Inf
1280 1303 Read as much as possible, returning a column vector.
1281 The argument @var{precision} is a string specifying the type of data to 1304
1282 read and may be one of @code{"char"}, @code{"schar"}, @code{"short"}, 1305 @item @var{nr}
1283 @code{"int"}, @code{"long"}, @code{"float"}, @code{"double"}, 1306 @itemx [@var{nr}, Inf]
1284 @code{"uchar"}, @code{"ushort"}, @code{"uint"}, or @code{"ulong"}. The 1307 Read as much as possible, returning a matrix with @var{nr} rows. If the
1285 default precision is @code{"uchar"}. 1308 number of elements read is not an exact multiple of @var{nr}, the last
1286 1309 column is padded with zeros.
1287 The @code{fread} function returns two values, @code{data}, which is the 1310
1288 data read from the file, and @code{count}, which is the number of 1311 @item [@var{nr}, @var{nc}]
1289 elements read. 1312 Read up to @code{@var{nr} * @var{nc}} elements, returning a matrix with
1290 @end deftypefn 1313 @var{nr} rows. If the number of elements read is not an exact multiple
1291 1314 of @var{nr}, the last column is padded with zeros.
1292 @deftypefn {Built-in Function} {} fwrite (@var{fid}, @var{data}, @var{precision}, @var{arch}) 1315 @end table
1293 This function writes data in binary form of type @var{precision} to the 1316
1294 specified @var{fid}, which may be either a file name, or a file id 1317 @noindent
1295 as returned from @code{fopen}. 1318 If @var{size} is omitted, a value of @code{Inf} is assumed.
1319
1320 The optional argument @var{precision} is a string specifying the type of
1321 data to read and may be one of
1322
1323 @table @code
1324 @item "char"
1325 @itemx "char*1"
1326 @itemx "integer*1"
1327 @itemx "int8"
1328 Single character.
1329
1330 @item "signed char"
1331 @itemx "schar"
1332 Signed character.
1333
1334 @item "unsigned char"
1335 @itemx "uchar"
1336 Unsigned character.
1337
1338 @item "short"
1339 Short integer.
1340
1341 @item "unsigned short"
1342 @itemx "ushort"
1343 Unsigned short integer.
1344
1345 @item "int"
1346 Integer.
1347
1348 @item "unsigned int"
1349 @itemx "uint"
1350 Unsigned integer.
1351
1352 @item "long"
1353 Long integer.
1354
1355 @item "unsigned long"
1356 @itemx "ulong"
1357 Unsigned long integer.
1358
1359 @item "float"
1360 @itemx "float32"
1361 @itemx "real*4"
1362 Single precision float.
1363
1364 @item "double"
1365 @itemx "float64"
1366 @itemx "real*8"
1367 Double precision float.
1368
1369 @item "integer*2"
1370 @itemx "int16"
1371 Two byte integer.
1372
1373 @item "integer*4"
1374 @itemx "int32"
1375 Four byte integer.
1376 @end table
1377
1378 @noindent
1379 The default precision is @code{"uchar"}.
1380
1381 The optional argument @var{skip} specifies the number of bytes to skip
1382 before each element is read. If it is not specified, a value of 0 is
1383 assumed.
1384
1385 The optional argument @var{arch} is a string specifying the data format
1386 for the file. Valid values are
1387
1388 @table @code
1389 @item "native"
1390 The format of the current machine.
1391
1392 @item "ieee-le"
1393 IEEE big endian.
1394
1395 @item "ieee-be"
1396 IEEE little endian.
1397
1398 @item "vaxd"
1399 VAX D floating format.
1400
1401 @item "vaxg"
1402 VAX G floating format.
1403
1404 @item "cray"
1405 Cray floating format.
1406 @end table
1407
1408 @noindent
1409 Conversions are currently only supported for @code{"ieee-be"} and
1410 @code{"ieee-le"} formats.
1411
1412 The data read from the file is returned in @var{val}, and the number of
1413 values read is returned in @code{count}
1414 @end deftypefn
1415
1416 @deftypefn {Built-in Function} {@var{count} =} fwrite (@var{fid}, @var{data}, @var{precision}, @var{skip}, @var{arch})
1417 Write data in binary form of type @var{precision} to the specified file
1418 ID @var{fid}, returning the number of values successfully written to the
1419 file.
1296 1420
1297 The argument @var{data} is a matrix of values that are to be written to 1421 The argument @var{data} is a matrix of values that are to be written to
1298 the file. The values are extracted in column-major order. 1422 the file. The values are extracted in column-major order.
1299 1423
1300 The argument @var{precision} is a string specifying the type of data to 1424 The remaining arguments @var{precision}, @var{skip}, and @var{arch} are
1301 read and may be one of @code{"char"}, @code{"schar"}, @code{"short"}, 1425 optional, and are interpreted as described for @code{fread}.
1302 @code{"int"}, @code{"long"}, @code{"float"}, @code{"double"},
1303 @code{"uchar"}, @code{"ushort"}, @code{"uint"}, or @code{"ulong"}. The
1304 default precision is @code{"uchar"}.
1305
1306 The @code{fwrite} function returns the number of elements written.
1307 1426
1308 The behavior of @code{fwrite} is undefined if the values in @var{data} 1427 The behavior of @code{fwrite} is undefined if the values in @var{data}
1309 are too large to fit in the specified precision. 1428 are too large to fit in the specified precision.
1310 @end deftypefn 1429 @end deftypefn
1311 1430
1364 @deftypefn {Built-in Function} {} ftell (@var{fid}) 1483 @deftypefn {Built-in Function} {} ftell (@var{fid})
1365 Return the position of the file pointer as the number of characters 1484 Return the position of the file pointer as the number of characters
1366 from the beginning of the file @var{fid}. 1485 from the beginning of the file @var{fid}.
1367 @end deftypefn 1486 @end deftypefn
1368 1487
1369 @deftypefn {Built-in Function} {} fseek (@var{fid}, offset, origin) 1488 @deftypefn {Built-in Function} {} fseek (@var{fid}, @var{offset}, @var{origin})
1370 Set the file pointer to any location within the file @var{fid}. The 1489 Set the file pointer to any location within the file @var{fid}. The
1371 pointer is positioned @code{offset} characters from the @code{origin}, 1490 pointer is positioned @var{offset} characters from the @var{origin},
1372 which may be one of the predefined variables @code{SEEK_CUR} (current 1491 which may be one of the predefined variables @code{SEEK_CUR} (current
1373 position), @code{SEEK_SET} (beginning), or @code{SEEK_END} (end of 1492 position), @code{SEEK_SET} (beginning), or @code{SEEK_END} (end of
1374 file). If @code{origin} is omitted, @code{SEEK_SET} is assumed. The 1493 file). If @var{origin} is omitted, @code{SEEK_SET} is assumed. The
1375 offset must be zero, or a value returned by @code{ftell} (in which case 1494 offset must be zero, or a value returned by @code{ftell} (in which case
1376 @code{origin} must be @code{SEEK_SET}. 1495 @var{origin} must be @code{SEEK_SET}.
1377 @end deftypefn 1496 @end deftypefn
1378 1497
1379 @defvr {Built-in Variable} SEEK_SET 1498 @defvr {Built-in Variable} SEEK_SET
1380 @defvrx {Built-in Variable} SEEK_CUR 1499 @defvrx {Built-in Variable} SEEK_CUR
1381 @defvrx {Built-in Variable} SEEK_END 1500 @defvrx {Built-in Variable} SEEK_END
1388 1 for success, and 0 if an error was encountered. It is equivalent to 1507 1 for success, and 0 if an error was encountered. It is equivalent to
1389 @code{fseek (@var{fid}, 0, SEEK_SET)}. 1508 @code{fseek (@var{fid}, 0, SEEK_SET)}.
1390 @end deftypefn 1509 @end deftypefn
1391 1510
1392 The following example stores the current file position in the variable 1511 The following example stores the current file position in the variable
1393 @samp{marker}, moves the pointer to the beginning of the file, reads 1512 @code{marker}, moves the pointer to the beginning of the file, reads
1394 four characters, and then returns to the original position. 1513 four characters, and then returns to the original position.
1395 1514
1396 @example 1515 @example
1397 marker = ftell (myfile); 1516 marker = ftell (myfile);
1398 frewind (myfile); 1517 frewind (myfile);