One method would be use 'mopen' as demonstrated below.
f=mopen('./data.txt','r'); for i=1:5 [n,x(i),y(i)]=mfscanf(f,'%f\t%f'); end mclose(f); |
Another method would be to read the entire file as a matrix.
M=fscanfMat('./data.txt'); |
This reads the entire data into matrix M. The functions fscanf are not efficient for reading files sequentially. 'fscanfMat('filename')' reads `filename' in such a way that first non-numeric lines of the file are ignored. You can also print matrices in ASCII format with `fprintfMat'. Both functions allow an exchange of information with many other programs or devices. Notice that `fscanfMat' only ignores the first non numeric lines. If non-numeric lines are mixed together with numeric values, this command will only read until it reaches a non-numeric line.
data = rand (20,5,'normal'); fprintfMat('test3.dat',data,'%15.10f'); |
fprintfMat cannot be used to write multiple matrices into the same file. So for every matrix, you need to have a separate file.
Yet another method is to use 'read' command:
fid = file ('open','test.dat','unknown') ; data = read (fid, -1, 5) file ('close' , fid) |
`file' is used to open and close the file and values are recovered by `read'. Each row is called a register. As we know that each register has five values, the third option indicates that each register is composed by five values. Since we do not know the number of registers we adjust the second parameter to `-1' and the file is read until the end of its content.
t = ascii(9); // tab character fid = file ('open', './position.txt', 'unknown'); fprintf(f1, '%f %f %f %f', t, x(1), x(2), theta_d); fprintf(f1, '%f %s %f', x(1), t, x(2)); file('close', fid); |
fprintf command does not identify '\t' as tab character. you need to use its ascii value instead. Each call to fprintf includes a newline character by default. One can use 'C - style' print commands as shown below. It is convenient while printing within loops.
x = 0.0:0.2:1; f1 = mopen('./test.txt','w');
for i = 1:5 mfprintf(f1, '%f\t', x(i)); end mfprintf(f1, '\n');
mclose(f1); |
-- 17 December 2007 Monday
21st December 2010, Tuesday
Following code deletes a existing file:
if(isfile('/path/to/file/input.txt'))
mdelete('/path/to/file/input.txt');
end
file(‘close’, file());
[TOC]
A script file is a text file containing SCILAB commands listed as you would have entered them in an interactive SCILAB session. A script file can also contain comment lines that are used to document the function. A script file typically has the suffix .sce. To execute a script file use the command exec.
A function file contains a scilab function definition and ends with a suffix .sci. To load a function into Scilab, use command getf.
A diary file is simply a text file that collects SCILAB commands and output from the point in which the command diary is invoked to the point where the command diary(0) is entered.
exec('./abc/file.sce'); // run a script getf('./abc/func.sci'); // load a function file diary('./abc/record.txt'); // start recording commands x = 0:0.2:1; diary(0); // end recording |
Use 'mode(-1)' in your script file to suppress command line output from the script.
Main window is the standard input and output device for scilab. it is refered to by variable %io. %io(1) is the standard input and %io(2) is the standard output. fprintf and fscanf can be used to read and write directly from/to this device using this variable.
-- 17 December 2007 Monday
[TOC]
If a Scilab function opens a file using mopen or file('open',...) it is required to close it using mclose or
file('close',...) in any case. In particular if an error occur between the open ant the close the file is not automatically closed. I assume that getd correctly handles open and close.
It is possible to close all the open files using the instruction file('close',file()) this is much more convenient than closing and restarting Scilab. -- Serge Steer, Inria
-- 18 December 2007 Tuesday
[TOC]