home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / maths / rlab / docs / RLEF45~1 < prev    next >
Encoding:
Text File  |  1996-07-02  |  12.9 KB  |  391 lines

  1. <HTML>
  2. <HEAD>
  3. <TITLE> Rlab2 Reference Manual: Input and Output</TITLE>
  4. </HEAD>
  5. <BODY>
  6. <A HREF="rlab-ref-4.html"><IMG SRC="prev.gif" ALT="Previous"></A>
  7. <A HREF="rlab-ref-6.html"><IMG SRC="next.gif" ALT="Next"></A>
  8. <A HREF="rlab-ref.html#toc5"><IMG SRC="toc.gif" ALT="Contents"></A>
  9. <HR>
  10. <H2><A NAME="s5">5. Input and Output</A></H2>
  11.  
  12.  
  13. <P>There are many ways to get data and programs in and out of
  14. Rlab. First we will discuss how <EM>file-handles</EM> are specified,
  15. and how they operate. Then we will cover program input, and quickly
  16. move on to data input and output.</P>
  17.  
  18. <H2><A NAME="ss5.1">5.1 File-Handles</A></H2>
  19.  
  20.  
  21. <P>File-handles are the mechanism through which the source of the
  22. input, or the destination of the output is specified. File-handles
  23. are deliberately simple; they are nothing more than strings. There
  24. are three pre-defined file-handles: </P>
  25. <P>
  26. <UL>
  27. <LI> <CODE>"stdin"</CODE> allows input from the standard input
  28. device. Typically, the keyboard.</LI>
  29. <LI> <CODE>"stdout"</CODE> allows output to the standard output
  30. device. Usually the terminal screen.</LI>
  31. <LI> <CODE>"stderr"</CODE> allows output to the standard error device,
  32. usually the same as the standard output, the terminal
  33. screen. </LI>
  34. </UL>
  35. </P>
  36. <P>Data can be read from or output to other devices or files by simply
  37. specifying an alternate file-handle. Files are the simplest, the
  38. file name is simply enclosed within double-quotes to make it a
  39. string. Any string will work: a string constant, a string variable,
  40. or and element of a string matrix. For example:</P>
  41. <P>
  42. <BLOCKQUOTE><CODE>
  43. <PRE>
  44. line = getline("file.input");
  45. </PRE>
  46. </CODE></BLOCKQUOTE>
  47. </P>
  48. <P>Will read a line of the file <CODE>file.input</CODE>.</P>
  49. <P>Functions that read or write data will automatically open files, so
  50. an explicit open function is not usually necessary, although one
  51. exists for special circumstances. Some functions will automatically
  52. close files when the function has finished its task, others
  53. won't. For example the function <CODE>readm</CODE> will read a single
  54. matrix from a file. When <CODE>readm</CODE> is finished, it will close the
  55. specified file. On the other hand, when <CODE>writem</CODE> is used it
  56. will not close the file in case the user want to keep writing data. </P>
  57. <P>Input and output can be performed from processes as well as
  58. files. In order to read or write from a process build a string that
  59. contains the process command. Make the first character of the
  60. command string a <CODE>|</CODE>. Rlab will run the command following the
  61. <CODE>|</CODE> reading the command's standard output, or writing to the
  62. command's standard input. The pipe to/from the process input/output
  63. will remain open until it is explicitly closed via the <CODE>close</CODE>
  64. function.</P>
  65. <P>This is a very handy capability for communicating with other
  66. programs. For example the an interface to the X-Geomview program can
  67. be written entirely in an rfile using process I/O. The file handle
  68. can be defined as:</P>
  69. <P>
  70. <BLOCKQUOTE><CODE>
  71. <PRE>
  72. GEOM = "|/usr/local/bin/geomview -c -";
  73. </PRE>
  74. </CODE></BLOCKQUOTE>
  75. </P>
  76. <P>The file handle is stored in a variable so it can easily be used
  77. more than once. Commands, and data can then be sent to X-Geomview
  78. with a statements like:</P>
  79. <P>
  80. <BLOCKQUOTE><CODE>
  81. <PRE>
  82. fprintf (GEOM, "%i  %i\n", ML.x.n, ML.y.n);
  83. </PRE>
  84. </CODE></BLOCKQUOTE>
  85. </P>
  86. <P>The X-Geomview process can be closed by:</P>
  87. <P>
  88. <BLOCKQUOTE><CODE>
  89. <PRE>
  90. close(GEOM);
  91. </PRE>
  92. </CODE></BLOCKQUOTE>
  93. </P>
  94.  
  95.  
  96. <H2><A NAME="ss5.2">5.2 Programs</A></H2>
  97.  
  98.  
  99. <P>Since Rlab offers an interactive mode of operation, programs can be
  100. entered from the command line. Programs can be stored in files, and
  101. loaded with either the <CODE>load</CODE> function, or the <CODE>rfile</CODE>
  102. command. Additionally, programs can be read from the standard input,
  103. or file names can be specified on the command line.</P>
  104.  
  105.  
  106. <H2><A NAME="reading-data"></A> <A NAME="ss5.3">5.3 Data </A></H2>
  107.  
  108.  
  109. <P>There are several methods available for reading and writing
  110. data. Detailed information is available for each function in the
  111. Builtin Function section of this manual, and in the online help. To
  112. summarize:</P>
  113. <P>
  114. <DL>
  115. <DT><B><CODE>write</CODE></B><DD><P>Write Rlab binary data files. <CODE>write</CODE>
  116. can write numeric and string matrices, and lists in compact
  117. binary form to a file. Since the byte-ordering is recorded,
  118. the file can be read on many other computers (IEEE-754
  119. compliant) .</P>
  120.  
  121. <DT><B><CODE>read</CODE></B><DD><P>Read Rlab binary data files. Rlab keeps track
  122. of byte-ordering on IEEE-754 compliant computers, so these
  123. binaries can be written, and subsequently read on different
  124. machines. The double-precision matrix structure is the same
  125. as Matlab's, so Rlab can read and write Matlab files
  126. containing matrices.</P>
  127.  
  128. <DT><B><CODE>writem</CODE></B><DD><P>Write a real-numeric matrix to a file in
  129. ASCII format (human-readable). The matrix is output row at
  130. a time, so that there are as many rows and column in the
  131. output file as there are in the matrix. Only real matrices
  132. are supported. To write a complex matrix the user must
  133. first write the real, and then the imaginary parts:</P>
  134. <P>
  135. <BLOCKQUOTE><CODE>
  136. <PRE>
  137. > writem("file.output", real(z));
  138. > writem("file.output", imag(z));
  139. </PRE>
  140. </CODE></BLOCKQUOTE>
  141. </P>
  142.  
  143. <DT><B><CODE>readm</CODE></B><DD><P>Read the an ASCII matrix from a
  144. file. Normally reads the output from <CODE>writem</CODE>, but can
  145. also read any text file that consists of white-space
  146. separated columns of numbers. Each row must contain the
  147. same number of columns. <CODE>readm</CODE> will take some
  148. optional arguments that give it some knowledge of the input
  149. file structure, and help it do a more efficient job.</P>
  150.  
  151. <DT><B><CODE>getline</CODE></B><DD><P>Reads a line of input. Default behavior is
  152. to read a line of input, then break the input into fields
  153. containing either numbers or strings, and return the
  154. fields, in a list, to the caller. <CODE>getline</CODE> behavior
  155. was patterned after AWK's own getline
  156. function. <CODE>getline</CODE> can also read entire lines as a
  157. string, which can then be split with the <CODE>strsplt</CODE>
  158. function. Often, the <CODE>getline</CODE> - <CODE>strsplt</CODE>
  159. combination is more efficient than <CODE>getline</CODE> itself.</P>
  160.  
  161. <DT><B><CODE>fread</CODE></B><DD><P>Read arbitrarily structured binary
  162. files. This function is patterned after the C-language
  163. fread. Of note is the argument that specifies the
  164. byte-ordering of the input file. This argument allows users
  165. to read files generated on different platforms.</P>
  166.  
  167. <DT><B><CODE>fprintf</CODE></B><DD><P>Formatted ASCII output. This function is
  168. patterned after the C-language fprintf.</P>
  169.  
  170. </DL>
  171. </P>
  172.  
  173. <H3>Examples</H3>
  174.  
  175.  
  176. <P>At this point some examples are probably most useful. We will focus
  177. on getting data into Rlab, since that is often the most
  178. troublesome.</P>
  179.  
  180. <H3>Readm Example</H3>
  181.  
  182.  
  183. <P><CODE>readm</CODE> reads blocks of white-space separated numbers in a
  184. file, and is useful for reading data from outside sources. Other
  185. programs may not generate data quite the way you (or <CODE>readm</CODE>)
  186. would like it, fortunately there are text-processing and formatting
  187. tools like AWK which are well suited to the purpose of re-arranging
  188. your data. In this example we will read differently formatted ASCII
  189. files. The simplest is a file formatted with the same number of
  190. columns per row, like so:</P>
  191. <P>
  192. <HR>
  193. <PRE>
  194. 1  2  3  4
  195. 5  6  7  8
  196. 9  10  11  12
  197. </PRE>
  198. <HR>
  199. </P>
  200. <P>This file can be read, row-wise, with the statement:
  201. <BLOCKQUOTE><CODE>
  202. <PRE>
  203. > x = readm("file1.in")
  204.         1          2          3          4  
  205.         5          6          7          8  
  206.         9         10         11         12  
  207. </PRE>
  208. </CODE></BLOCKQUOTE>
  209. </P>
  210. <P>That is, each row of the input file is read, and becomes a row of
  211. the resulting matrix.  The same file can also be read column-wise by
  212. specifying the number of rows and columns to be read:</P>
  213. <P>
  214. <BLOCKQUOTE><CODE>
  215. <PRE>
  216. > x = readm("file1.in", [3, 4])
  217.         1          4          7         10  
  218.         2          5          8         11  
  219.         3          6          9         12  
  220. > x = readm("file1.in", [4, 3])
  221.         1          5          9  
  222.         2          6         10  
  223.         3          7         11  
  224.         4          8         12  
  225. </PRE>
  226. </CODE></BLOCKQUOTE>
  227. </P>
  228. <P>Actually, the file is still read row-wise, but the matrix is filled
  229. column by column according to the row and column specification in
  230. the second argument.</P>
  231. <P>Now for something a little trickier. Suppose you have the following
  232. file:
  233. <HR>
  234. <PRE>
  235. 1   2   3   4
  236. 5   6   7   8
  237. 9  10  11 
  238.  
  239. 12  13  14  15
  240. 16  17  18  19
  241. </PRE>
  242. <HR>
  243. </P>
  244. <P>If you use <CODE>readm</CODE> without giving it some help, it will not
  245. read all of that data.</P>
  246. <P>
  247. <BLOCKQUOTE><CODE>
  248. <PRE>
  249. > x = readm("file2.in")
  250.         1          2          3          4  
  251.         5          6          7          8  
  252.         9         10         11         12  
  253.        13         14         15         16  
  254. </PRE>
  255. </CODE></BLOCKQUOTE>
  256. </P>
  257. <P><CODE>readm</CODE> misses some of the data because it assumes each row of
  258. the input file has the same number of columns. If you give it a
  259. little help by telling it how many elements to read it will get them
  260. all. </P>
  261. <P>
  262. <BLOCKQUOTE><CODE>
  263. <PRE>
  264. > x = readm("file2.in", [1, 19])
  265.  matrix columns 1 thru 6
  266.         1          2          3          4          5          6  
  267.  
  268.  matrix columns 7 thru 12
  269.         7          8          9         10         11         12  
  270.  
  271.  matrix columns 13 thru 18
  272.        13         14         15         16         17         18  
  273.  
  274.  matrix columns 19 thru 19
  275.        19  
  276. </PRE>
  277. </CODE></BLOCKQUOTE>
  278. </P>
  279.  
  280. <H3>Getline Example</H3>
  281.  
  282.  
  283. <P><CODE>getline</CODE> is a useful tool for dealing with many types of
  284. inputs. It is not always the most efficient, its strength lies in
  285. ease of use. A few common uses of <CODE>getline</CODE> will be
  286. show. First, the simplest usage:</P>
  287. <P>
  288. <BLOCKQUOTE><CODE>
  289. <PRE>
  290. > printf("Input something > "); ans = getline("stdin");
  291. Input something > a number 12.73e2
  292. > ans
  293.    1            2            3            
  294. > ans.[1]
  295. a  
  296. > ans.[2]
  297. number  
  298. > ans.[3]
  299.  1.27e+03
  300. </PRE>
  301. </CODE></BLOCKQUOTE>
  302. </P>
  303. <P>The <CODE>printf</CODE> statement creates the prompt: <CODE>Input something
  304. ></CODE>, and the <CODE>getline</CODE> statement reads the entire line of
  305. input, splitting the line into fields separated by whitespace. Each
  306. field, either a number or a string is stored in the returned list,
  307. <CODE>ans</CODE>. The rest of the example just exposes the contents of the
  308. list. </P>
  309. <P>The next simple example shows how to use <CODE>getline</CODE> to read from
  310. a file until the end-of-file (EOF) is reached.  When <CODE>getline</CODE>
  311. encounters the end-of-file it returns a list with zero length. Thus
  312. the <CODE>while</CODE> loop will execute until end-of-file.</P>
  313. <P>
  314. <BLOCKQUOTE><CODE>
  315. <PRE>
  316. while (length (ans = getline("file1.in"))) 
  317. {
  318.    // Do something with each line...
  319. }
  320. </PRE>
  321. </CODE></BLOCKQUOTE>
  322. </P>
  323. <P>Since <CODE>getline</CODE> is operating within a loop, its return value,
  324. <CODE>ans</CODE> is overwritten each time the loop is executed. If the
  325. contents of the file are to be saved for later use this must be done
  326. within the loop. The following example shows how this might be
  327. done. Here <CODE>getline</CODE> is used with a second argument that
  328. specifies that the entire line be returned as a string.</P>
  329. <P>
  330. <BLOCKQUOTE><CODE>
  331. <PRE>
  332. svec = [];
  333. while (class (line = getline (FN, 0)) == "string")
  334. {
  335.   svec = [svec; line];
  336. }
  337. </PRE>
  338. </CODE></BLOCKQUOTE>
  339. </P>
  340.  
  341.  
  342. <H3>Getline / Strsplt Example</H3>
  343.  
  344.  
  345. <P>Reading in one type of data is most efficient with
  346. <CODE>getline(FN,LL)</CODE> usage. That is, you tell <CODE>getline</CODE> to
  347. read in the entire line as a string. Then you can use <CODE>strsplt</CODE>
  348. to divide up the line most efficiently. This method is often more
  349. efficient, because the combination of <CODE>getline</CODE> and
  350. <CODE>strsplt</CODE> do less work because you guide them through the
  351. process. If you force getline to split each line, it must examine
  352. every character on the line itself. For example, you might have a
  353. data file that looks like:</P>
  354. <P>
  355. <BLOCKQUOTE><CODE>
  356. <PRE>
  357. 123 456 12  14 15
  358. 1 15 15 16 22 99 22
  359. 22 22 33 44 55 66
  360. </PRE>
  361. </CODE></BLOCKQUOTE>
  362. </P>
  363. <P>It would be best to read this data with a small program that looked
  364. like:</P>
  365. <P>
  366. <BLOCKQUOTE><CODE>
  367. <PRE>
  368. while ( class (line = getline("data", -1)) )
  369. {
  370.   x = strtod (strsplt(line, " "));
  371.   # Do something with the data here...
  372. }
  373. </PRE>
  374. </CODE></BLOCKQUOTE>
  375. </P>
  376. <P>The key here is intelligent use of <CODE>strsplt</CODE> and
  377. <CODE>strtod</CODE>. <CODE>strsplt</CODE> breaks the string into pieces using
  378. field separators specified in the second argument. <CODE>strtod</CODE>
  379. converts its string argument to a number.</P>
  380.  
  381.  
  382.  
  383.  
  384.  
  385. <HR>
  386. <A HREF="rlab-ref-4.html"><IMG SRC="prev.gif" ALT="Previous"></A>
  387. <A HREF="rlab-ref-6.html"><IMG SRC="next.gif" ALT="Next"></A>
  388. <A HREF="rlab-ref.html#toc5"><IMG SRC="toc.gif" ALT="Contents"></A>
  389. </BODY>
  390. </HTML>
  391.