|
Contents | Previous | Next | Subchapters |
| Syntax |
read(file, mtype)
|
| See Also | exists , nrows , readh , write , file2str , strreplace , bread , spread , xlread , and read(file, mtype, nr) |
If a line of a file is empty,
read returns an empty matrix for that line.
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 ]
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 ]
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 ]
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 ]
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.
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) ]
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.