Scilab/C4/File-handling/English
Title of script: File Handling in Scilab
Author: Anuradha Amrutkar
Keywords: File Handling, writefile, read file, open file, close file
|
|
---|---|
Slide | Welcome to this spoken tutorial on File Handling using Scilab. |
Slide | In this tutorial, we will learn
|
Slide | Pre-requisites:
You should have
|
Slide | System Requirements
For Demonstration: Ubuntu Linux12.04 operating system is used with Scilab version 5.3.3 installed |
Slide | Now we will see some functions in scilab which are useful for File Handling.
File Handling involves-
|
Let us start with writing data to a file.
For this purpose the write() command is used. | |
Scilab Console
0.7560439 0.0002211 0.3303271 0.6653811 0.6283918 0.8497452 0.6857310 0.8782165 0.0683740 0.5608486 0.6623569 0.7263507 0.1985144 0.5442573 0.2320748 0.2312237 0.2164633 0.8833888 0.6525135 |
To begin with let us create a matrix of random numbers.
|
Scilab Console
|
Now check the present working directory. Type
ans =
Make sure that you are in a directory where you have read & write permission , before you execute these commands. |
--> write('random-numbers.txt', random_matrix) | Now we will write the content of variable random underscore matrix into a text file using the write command.
--> write inside bracket open single quotes random dash numbers dot txt close single quotes comma random underscore matrix |
Let us see whether this file has been created.
I will minimize the scilab console window.
| |
You can see the data from variable random underscore matrix is written to a text file radom dash numbers dot txt | |
Scilab Console
0.2113249 0.7560439 0.0002211 0.3303271 0.6653811 0.6283918 0.8497452 0.6857310 0.8782165 0.0683740 0.5608486 0.6623569 0.7263507 0.1985144 0.5442573 0.2320748 0.2312237 0.2164633 0.8833888 0.6525135 |
Now we will see how to read the data from a file.
|
Narration:
-->new_vector = read('random-numbers.txt', 19, 1) |
If we modify the above command as
|
Slide | mopen:
r = Opens the file for reading. rb = Opens a binary file for reading. rt = Opens a text file for reading. w = Creates a new file for writing, or opens and truncates a file to zero length. wb = Creates a new binary file for writing, or opens and truncates a file to zero length. wt = Creates a text binary file for writing, or opens and truncates a file to zero length. a or ab = Appends (opens a file for writing at the end of the file, or creates a file for writing). r+ or r+b = Opens a file for update (reading and writing) |
Scilab Console
-->fd_r = mopen('random-numbers.txt','rt') fd_r = 1. |
For eg switching to the scilab console type
fd underscore r is equal to mopen('random-numbers.txt','rt') the above command opens the 'random-numbers' as text and read-only mode. |
Slide | mclose:
mclose command closes the opened file by using mopen. mclose(fd)
|
Scilab Console
-->mclose(fd_r) ans = 0. |
mclose inside bracket fd_r
|
Summary | That's all we have in this tutorial.
Let us summarize what we learnt -
Write Function Read Function mopen Function mclose Function |
Slide | Watch the video available at the link shown below
It summarises the Spoken Tutorial project If you do not have good bandwidth, you can download and watch it |
Spoken Tutorial Workshops | The Spoken Tutorial Project Team
Conducts workshops using spoken tutorials Gives certificates to those who pass an online test For more details, please write to |
Acknowledgement | Spoken Tutorial Project is a part of the Talk to a
Teacher project It is supported by the National Mission on Education through ICT, MHRD, Government of India More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro |
This is Anuradha Amrutkar from IIT Bombay
Thank You for joining |