4.1.3 Input and Output

Although the commands save and load are convenient, one has much more control over the transfer of data between files and Scilab by using the Fortran like functions read and write. These two functions work similarly to the read and write commands found in Fortran. The syntax of these two commands is as follows.

 
--> x=[1 2 %pi;%e 3 4]
 x         =
 
!   1.           2.    3.1415927 !
!   2.7182818    3.    4.        !
 
--> write('x.dat',x)
 
--> clear x
 
--> xnew=read('x.dat',2,3)
 xnew      =
 
!   1.           2.    3.1415927 !
!   2.7182818    3.    4.        !
Notice that read specifies the number of rows and columns of the matrix x. Complicated formats can be specified.

The C like function mfscanf and mfprintf can be also used

 
--> x=[1 2 %pi;%e 3 4]
 x         =
 
!   1.           2.    3.1415927 !
!   2.7182818    3.    4.        !
 
--> fd=mopen('x_c.dat','w')

--> mfprintf(fd,'%f %f %f\n',x)

--> mclose(fd)
 
--> clear x
 
--> fd=mopen('x_c.dat','r')

--> xnew(1,1:3)=mfscanf(fd,'%f %f %f\n') ;

--> xnew(2,1:3)=mfscanf(fd,'%f %f %f\n')

 xnew  =
 
!   1.          2.    3.141593 !
!   2.718282    3.    4.       !
--> mclose(fd)