Contents Previous Next Subchapters

Reading The Next Row From A File
Syntax read(filemtype)
See Also exists , nrows , readh , write , file2str , strreplace , bread , spread , xlread , and read(file, mtype, nr)

Description
Returns a row vector of the specified type containing the data from the current marker position in the specified file to the end of the current row, where file is a character row vector, and mtype is "logical", "char", "int", "real", "double", "complex" or "byte".

If a line of a file is empty, read returns an empty matrix for that line.

Example
Each file has a marker that points to the next line in the file. The marker starts at the first line of the file and moves top to bottom.

Integer
If mtype is "int", the lines read from file can only contain integer values. You can create a file called temp.dat that contains the text
     1  2
     3  4
by entering the following commands
     clear
     file = "temp.dat"
     rmfile(file);
     write(file, "1 2")
     write(file, "3 4")
     close(file)
If you then enter
     read("temp.dat", "int")
O-Matrix reads the first line of temp.dat and responds
     [ 1 , 2 ]
The file marker now points to the second line of temp.dat. If you continue the previous example by entering
     read("temp.dat", "int")
O-Matrix will respond
     [ 3 , 4 ]

Real
If mtype is "real", the lines read from file can contain integer and real values. You can create a file called temp.dat that contains the text
     3.3 5
by entering the following commands
     clear
     file = "temp.dat"
     rmfile(file);
     write(file, "3.3 5")
     close(file)
If you then enter
     read("temp.dat", "real")
O-Matrix will respond
     [ 3.3 , 5 ]

Double
If mtype is "double", the lines read from file can contain integer, real, and double-precision values. You can create a file called temp.dat that contains the text
     3.3 5 2.5d0
by entering the following commands
     clear
     file = "temp.dat"
     rmfile(file);
     write(file, "3.3 5 2.5d0")
     close(file)
If you then enter
     read("temp.dat", "double")
O-Matrix will respond
     [ 3.3 , 5 , 2.5 ]

Logical
If mtype is "logical", the lines read from file can only contain the values "T" and "F". You can create a file called temp.dat that contains the text
     T  F  T
by entering the following commands
     clear
     file = "temp.dat"
     rmfile(file);
     write(file, "T F T")
     close(file)
If you then enter
     read("temp.dat", "logical")
O-Matrix will respond
     [ T , F , T ]

Character
If mtype is "char" or "byte", the lines are read from the file as ascii characters. You can create a file called temp.dat that contains the text
     The bright red fox
     jumped over the dull white fence.
by entering the following commands
     clear
     file = "temp.dat"
     rmfile(file);
     write(file, "The bright red fox")
     write(file, "jumped over the dull white fence.")
     close(file)
If you then enter
     line1 = read("temp.dat", "char")
     line2 = read("temp.dat", "char")
     print line2
O-Matrix will respond
     jumped over the dull white fence.

Complex
If mtype is "complex", the lines read from file can contain integer, real, or double-precision numbers grouped in pairs. Each pair is enclosed by parentheses, with the real and imaginary parts separated by a comma. You can create a file called temp.dat that contains the text
     (1,2) (3,4)
by entering the following commands
     clear
     file = "temp.dat"
     rmfile(file);
     write(file, "(1,2) (3,4)")
     close(file)
If you then enter
     x = read("temp.dat", "complex")
     print 2 * x
O-Matrix will respond
     [ (2,4) , (6,8) ]

Ellipsis
In all of the examples above each row of the matrix resulting from a read corresponded to one line of the file. An ellipsis can be used to continue a row on the next line of a file. You can create a file called temp.dat that contains the text
     The bright red fox...
     jumped over the dull white fence.
by entering the following commands
     clear
     file = "temp.dat"
     rmfile(file);
     write(file, "The bright red fox...")
     write(file, "jumped over the dull white fence.")
     close(file)
If you then enter
     line1 = read("temp.dat", "char")
     print line1
O-Matrix will respond
     The bright red fox jumped over the dull white fence.
On the other hand this interpretation of the ellipsis can be suppressed. If you continue by entering
     line1 = read("temp.dat", "byte")
     print line1
O-Matrix will respond
     The bright red fox...
The ellipsis can also be used for comments because the read function ignores characters from the ellipsis to the end of the line. (In actual fact the text from ellipsis to the end of the line is replaced by a single space.) You can create a file called temp.dat that contains the text
     ... This entire line is ignored
     The bright red fox... This text is ignored
     jumped over the dull white fence.
by entering the following commands
     clear
     file = "temp.dat"
     rmfile(file);
     write(file, "... This entire line is ignored")
     write(file, "The bright red fox... This text is ignored")
     write(file, "jumped over the dull white fence.")
     close(file)
If you then enter
     line1 = read("temp.dat", "char")
     print line1
O-Matrix will respond
     The bright red fox jumped over the dull white fence.
Note that the ellipsis and the rest of the characters on the line are replaced by a single space.