How to Handle Files with Scilab on Ubuntu 15.04
Scilab is an OpenSource Linux software for numerical computation similar to Matlab. This tutorial shows how to load data from files into Scilab for later use or processing. Scilab will interpret the code in the file and it's structure and format, etc. To use a file within the Scilab environment, it is necessary to use a number of previous commands that allow both reading and interpretation of the file in question.
You haven't installed scilab yet? Please see
Opening Files with the mopen command
This command opens a file in Scilab. The sequence is:
[fd, err] = mopen(file [, mode, swap ])
The meaning for each argument is:
File: A character string containing the path of the file to open.
Mode: A character string specifying the access mode requested for the file
Swap: A scalar swap is present and swap = 0 then automatic bytes swap is disabled. The default value is 1.
Err: Returns a value that indicates the following errors:
Error Value | Error Message |
0 | No error |
-1 | No more logical Units |
-2 | Cannot open file |
-3 | No more memory |
-4 | Invalid value |
-5 | Invalid Status |
Fd: a positive integer, indicates a file descriptor.
Example Opening Files in Ubuntu Using Scilab
Now, we are going to open a MS Word Document using de mopen command
[fd, err] = mopen('/home/david/Documentos/Celestron Ubuntu.docx')
Please note that we didn´t use any additional argument, only for opening purpose.
Note: In the Variable Browser we can find all the variable created including fd.
Parameters in mode Argument
The parameters are used to control access to the stream. The possible values are:
r: Opens the file for reading purposes.
rb: Opens the binary file for reading.
rt: Opens a text file for reading.
w: Creates a new file for writing. Also truncates the actual file to zero length.
wb: Creates a new binary file for writing. Also truncates the actual file to zero length.
wt: Creates a new text binary file for writing. Also truncates the actual file to zero length.
a or ab: Appends writing to opened file to the end.
r+ or r+b: Opens a file for update.
w+ or w+b: Truncates to zero length or creates a new file for update.
a+ or a+b: Appends.
Example Opening Files with parameters in Ubuntu Using Scilab
In this example, we are going to create a text file and write a line on it.
Type:
[fd, err] = mopen('/home/your name/test.txt', 'wt' );
mputl('Line text for test purposes', fd);
Note that if we have finished working with the file that we created, we have to close it using the mclose command. Later in this tutorial we try mclose command syntax.
mclose (fd);
Then we can search for the file in the directory:
Open the file:
This is useful if we are going to retrieve data from an external source, just like a data acquisition interface. We can load data from a txt file and then use this for processing.
Closing Files. mclose command.
Mclose must be used to close a file opened by mopen. If fd is omitted mclose closes the last opend file. mclose('all') closes all files opened by file('open',..) or mopen. Be careful with this use of mclose because when it is used inside a Scilab script file, it also closes the script and Scilab will not execute commands written after mclose('all').
Reading and using a text file content.
Sometimes we need to read and use the content of a txt file, either for reasons of data acquisition or for word processing. For reading purposes, we will use the command mgetl.
The Command mgetl
The command mgetl reads a line or lines from a txt file.
Syntax
txt=mgetl(file_desc [,m])
Arguments
file_desc: A character string giving the file name or a logical unit returned by mopen.
m: An integer scalar. The number of lines to read. The default value is -1.
txt: A column vector of string.
Examples using mgetl
With the file created before we can type:
>fd=mopen(/home/david/test.txt', 'r')
>txt=mgetl(fd,1);
>txt
>mclose(fd);
Note: We used the argument 'r' because we only need to read the file. A file cannot be opened for reading and writing at the same time. We set the argument 1 in mgetl for reading the first line only and don´t forget to close the file with mclose. The content of the first line is stored in a 'txt' string type variable.
There are many advanced commands that will be treated in further tutorials.
References
- Scilab Help Online, "https://help.scilab.org/". Retrieved at 06/30/2015.