Read objects from a file
This functionality does not run in MATLAB.
finput(filename | n) finput(filename | n, x1, x2, …)
finput(filename, x) reads a MuPAD® object from a file and assigns it to the identifier x.
finput(n, x) reads from the file associated with the file descriptor n.
finput can read MuPAD binary files as well as ASCII text files. finput recognizes the format of the file automatically.
Binary files may be created via fprint or write. Text files can also be created in a MuPAD session via these functions (using the Text option; see the corresponding help pages for details). Alternatively, text files can be created and edited directly using your favourite text editor. The file must consist of syntactically correct MuPAD objects or statements, separated by semicolons or colons. An object may extend over more than one line.
finput(filename) reads the first object in the file and returns it to the MuPAD session.
finput(filename, x1, x2, ...) reads the contents of a file object by object. The i-th object is assigned to the identifier xi. The identifiers are not evaluated while executing finput; previously assigned values are overwritten. The objects are not evaluated. Evaluation can be enforced with the function eval. Cf. Example 2.
Instead of a file name, also a file descriptor n of a file opened via fopen can be used. The functionality is as described above. However, there is one difference: With a file name, the file is closed automatically after the data were read. A subsequent call to finput starts at the beginning of the file. With a file descriptor, the file remains open (use fclose to close the file). The next time data are read from this file, the reading continues at the current position. Consequently, a file descriptor should be used if the individual objects in the file are to be read via several subsequent calls of finput. Cf. Example 3.
Files in gzip compressed format with a filename ending in ".gz" are automatically and transparently decompressed while reading.
If the number of identifiers specified in the finput call is larger than the number of objects in the file, the exceeding identifiers are not assigned any values. In such a case, finput returns the void object of type DOM_NULL.
finput interprets the file name as a pathname relative to the "working directory."
Note that the meaning of "working directory" depends on the operating system. On Windows® systems and on Mac OS X systems, the "working directory" is the folder where MuPAD is installed. On UNIX® systems, it is the current working directory in which MuPAD was started; when started from a menu or desktop item, this is typically the user's home directory.
Also absolute path names are processed by finput.
Create a new file in the system's temporary folder. The name of the temporary folder varies for different platforms. The fopen command with the TempFile option creates a file in any system's temporary folder (if such folder exists):
fid := fopen(TempFile, Write, Text):
Write the numbers 11, 22, 33 and 44 into a file:
fprint(fid, 11, 22, 33, 44):
Use fname to return the name of the temporary file you created:
file := fname(fid):
Read this file with finput:
finput(file, x1, x2, x3, x4)
x1, x2, x3, x4
If you try to read more objects than stored in the file, finput returns the void object of type DOM_NULL:
finput(file, x1, x2, x3, x4, x5); domtype(%)
delete x1, x2, x3, x4:
Objects read from a file are not evaluated:
fid := fopen(TempFile, Write, Text): file := fname(fid): fprint(file, x1): x1 := 23: finput(file)
We read some data from a file using several calls of finput. We have to use a file descriptor for reading from the file. The file is opened for reading with fopen:
fid := fopen(TempFile, Write, Text): fprint(fid, 11, 22, 33, 44): file := fname(fid): n := fopen(file):
The file descriptor returned by fopen can be passed to finput for reading the data:
finput(n, x1, x2): x1, x2
finput(n, x3, x4): x3, x4
Finally, we close the file and delete the identifiers:
fclose(n): delete n, x1, x2, x3, x4:
Alternatively, the contents of a file can be read into a MuPAD session in the following way:
n := fopen(file): for i from 1 to 4 do x.i := finput(n) end_for: x1, x2, x3, x4
fclose(n): delete n, i, x1, x2, x3, x4:
fid := fopen(TempFile, Write, Text): fprint(fid, 11, 22, 33): file := fname(fid): finput(file, (x1, x2), x3)
Error: The argument is invalid. [finput]
The following call does not lead to an error because the identifier x12 is not evaluated. Consequently, only one object is read from the file and assigned to x12:
x12 := x1, x2: finput(file, x12): x1, x2, x12
The name of a file: a character string
A file descriptor provided by fopen: a positive integer
x1, x2, …