Session 12: File operations [June 11, 2011, 1:14 p.m.]
Objectives
In this session we will learn the following:
- Writing formatted output to Scilab console
- Reading data from files into Scilab variables
- Writing formatted output to a file
Functions Used
mopen(), mprintf(), mfprintf(), msprintf(), mfscanf(), msscanf()
Introduction
Scilab file operations are similar to those in C programming language. If you are familiar with file operations in C, you will find it easy to perform file operations in Scilab. But this session does not assume you know C.
For both input and output, you will find a set of three similar functions - output to standard output (or input from standard input), output to (or input from) a file and output to (or input from) a string.
To write to (or read from) a file, it is necessary to first open the file for writing (or reading), perform the write (or read) operations and finally close the file.
Output Functions
mprintf() - Output to Standard Output (Scilab Console)
Let us first write scalar values first and then see how to write matrices.
-->mprintf("%5d, %12.3f\n", 120, 1234.55)
120, 1234.55
Notice what happened:
- To the function mprintf(), we provided three arguments. First, a string containing formatting specifications ("%5d, %12.3f\n"). Second is an integer (120). Third is a real number (1234.55)
- The formatting specification contains two format placeholders, namely, %5d and %12.3f. Format placeholders begin with the %. Rest of the formatting specification string are output as they are.
- %5d corresponds to the second argument to mprintf(), namely 120. It outputs 120 as an integer (%d), formatting it is a width of 5 places (%5d)
- %12.3f can be understood as follows - 12 is the width, 3 is the precision (number of decimal places) and f corresponds to a float (real number).
- If there are two placeholders in the formatting specification string, it must be followed by two values with the matching data type as in the formatting specification.
To print out values of a matrix, the format specification string must be able to print one full row of the matrix. This specification will be used for each row of the matrix until all rows are printed. It is an error to provide too few or too many placeholders in the format specification string than the number of columns being printed. Let us see an example:
-->a = [1 2 3 4; 5 6 7 8];
-->mprintf("%5.2f,%5.2f,%5.2f,%5.2f\n", a)
1.00, 2.00, 3.00, 4.00
5.00, 6.00, 7.00, 8.00
-->b = [10 11; 20 21; 30 31]; // 3x2 matrix
-->mprintf("%5.2f,%5.2f,%5.2f,%5.2f\t%8.3f,%8.3f\n", a, b)
1.00, 2.00, 3.00, 4.00 10.000, 11.000
5.00, 6.00, 7.00, 8.00 20.000, 21.000
Note that only two rows of matrix b are printed and not all the three. When printing values from more than one matrix, only that many rows are printed as the minimum number of rows in the matrices being printed. In the abov case, a is of size 2x4 and b is of size 3x2. Hence only 2 rows are printed from each matrix. The formatting specification string must contain enough placeholders required to print all the columns of all matrices being printed, which is 4 + 2 = 6 in the above case.
msprintf() - Output to a String
Function msprintf() is similar to mprintf(), except that the output is sent to a string variable (for storage and subsequent output) instead of to the standard output. The above output statements could be rewritten for output to a string as follows:
-->n = 20; s1 = msprintf("Number of items = %d\n", n);
-->disp(s1)
Number of items = 20
a = [1 2 3 4; 5 6 7 8];
-->s2 = msprintf("%5.2f,%5.2f,%5.2f,%5.2f\n", a)
-->disp(s2)
! 1.00, 2.00, 3.00, 4.00 !
! !
! 5.00, 6.00, 7.00, 8.00 !
mfprintf() - Output to a File
Function mfprintf() send the output to a file. Its use is similar to mprintf() and msprintf(), except that:
-
The file to which output is to be sent must first be opened using the mopen() function. If this function call is successful, it returns a file descriptor which must be used in all subsequent write operations to the file. It requires two input arguments:
-
A string containing the name of the file to be opened - A string specifying the type of file operation to be performed on the file after opening
-
- The mfprintf() takes one argument more than mprintf() and msprintf(). The first argument must be the file descriptor obtained with the mfopen() function.
- When all output to the file are complete, the file must be closed using mclose() function.
-->n = 20; a = [1 2 3 4; 5 6 7 8];
-->fd = mopen("test.dat", "w"); // Creates a new file or opens an existing file and truncates to zero length
-->mfprintf(fd, "Number of items = %d\n", n)
-->mfprintf(fd, %5.2f,%5.2f,%5.2f,%5.2f\n", a)
-->mclose(fd)
Note the current working directory, then search and open the file test.dat in an editor. It will containg the following lines:
Number of items = 20
1.00, 2.00, 3.00, 4.00
5.00, 6.00, 7.00, 8.00