Difference between revisions of "Scilab/C4/File-handling/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 20: Line 20:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn  
  
* '''File Handling'''  
+
* '''File Handling''' with the following functions;
 
** '''Write Function'''
 
** '''Write Function'''
 
** '''Read''' '''Function'''
 
** '''Read''' '''Function'''
 
** '''mopen Function'''
 
** '''mopen Function'''
 
** '''mclose Function'''
 
** '''mclose Function'''
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| System Requirements
 +
 +
For Demonstration i am using '''Ubuntu Linux12.04''' operating system with '''Scilab '''version 5.3.3 installed
  
  
Line 35: Line 40:
  
 
# Basic knowledge of '''Scilab'''.
 
# Basic knowledge of '''Scilab'''.
# If not, for relevant tutorials please visit '''spoken hyphen tutorial dot org'''
+
# If not, for relevant spoken tutorials please visit '''spoken hyphen tutorial dot org'''
  
  
  
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| System Requirements
 
 
For Demonstration: '''Ubuntu Linux12.04''' operating system is used with '''Scilab '''version 5.3.3 installed
 
  
 
|-
 
|-
Line 51: Line 51:
 
'''File Handling''' involves-  
 
'''File Handling''' involves-  
  
* writing to a file,
+
* writing to a file using write function,
* reading from a file,  
+
* reading from a file using read function,  
* open an existing file and  
+
* open an existing file using mopen function and  
* closing the opened file
+
* closing an already opened file using mclose function.
  
  
Line 113: Line 113:
  
 
'''0.6525135 '''
 
'''0.6525135 '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To begin with let us create a matrix of random numbers.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Switch to the scilab console window. To begin with let us create a matrix of random numbers. Type;
  
  
'''-->random underscore matrix is equal to rand inside bracket 20 comma 1'''
+
'''-->random underscore matrix is equal to rand in the  bracket 20 comma 1 close the bracket semicolon and press enter'''
  
  
Line 149: Line 149:
  
  
  '''--> write inside bracket open single quotes random dash numbers dot txt close single quotes comma random underscore matrix'''
+
  Type;'''--> write into bracket into quotes random dash numbers dot txt close the  quotes comma random underscore matrix close the bracket and press enter'''
  
 
|-
 
|-
Line 158: Line 158:
  
  
And open the file which will be created and saved in the '''home''' directory of my computer.
+
And open the file which will be created and saved in the '''fossee''' directory of my computer.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| You can see the data from variable '''random underscore matrix '''is written to a text file''' radom dash numbers dot txt'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| You can see the data from variable '''random underscore matrix '''is written in a text file''' random dash numbers dot txt'''
  
 
|-
 
|-
Line 212: Line 212:
  
 
0.6525135  
 
0.6525135  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will see how to read the data from a file.  
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I will close this file. Coming back to the scilab console. Now we will see how to read the data from a file.  
  
  
Line 218: Line 218:
  
  
'''new underscore vector is equal to read open bracket open single quote random dash numbers Dot txt close single quote comma 20 comma 1 close bracket'''
+
Type'''new underscore vector is equal to read into bracket in to  quote random dash numbers Dot txt close the quote comma 20 comma 1 close the  bracket and press enter.'''
  
  
Line 227: Line 227:
  
  
And stores in the variable '''new underscore vector '''
+
And stores in the variable '''new underscore vector ''' Press Enter to continue the display
  
 
|-
 
|-
Line 236: Line 236:
  
  
'''new underscore vector is equal to read open bracket open single quote randomdash numbers Dot txt close single quotecomma 19 comma 1 close bracket'''
+
'''new underscore vector is equal to read into  bracket into  quote random dash numbers Dot txt comma 19 comma 1 '''
  
  
The '''read''' command reads only 19 data values from the file mentioned in the argument.
+
The '''read''' command reads only 19 data values from the file mentioned in this argument.
  
  
Line 252: Line 252:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''mopen''':
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us see about the'''mopen''' function:
  
  
Line 269: Line 269:
 
'''rt '''<nowiki>= Opens a text file for reading.</nowiki>
 
'''rt '''<nowiki>= Opens a text file for reading.</nowiki>
  
'''w '''<nowiki>= Creates a new file for writing, or opens and truncates a file to zero length. </nowiki>
+
'''w '''<nowiki>= Creates a new file for writing, or opens and truncates a file to zero length. </nowiki>
  
 
'''wb '''<nowiki>= Creates a new binary file for writing, or opens and truncates a file to zero length.</nowiki>
 
'''wb '''<nowiki>= Creates a new binary file for writing, or opens and truncates a file to zero length.</nowiki>
Line 286: Line 286:
  
 
     '''1. '''
 
     '''1. '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| For eg switching to the scilab console type
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| For eg  
  
  '''fd underscore r is equal to mopen('random-numbers.txt','rt')'''
+
  '''fd underscore r is equal to mopen('random-numbers,'rt')'''
  
the above command opens the 'random-numbers' as text and read-only mode.
+
the above command opens the 'random-numbers' as a text and read-only mode.
  
 
|-
 
|-
Line 314: Line 314:
  
 
0.
 
0.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''mclose inside bracket fd_r'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''mclose into bracket fd'''closes the opened file by using mopen
 +
 
 +
Where fd is the file descriptor of the opened file.
  
 +
If fd is omitted mclose closes the last opened file.
  
the above command closes the 'random-numbers.txt' file.
+
Thats all we have in this tutorial.
  
 
|-
 
|-
Line 323: Line 326:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| That's all we have in this tutorial.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| That's all we have in this tutorial.
  
Let us summarize what we learnt -  
+
You have learnt -  
  
 
* File Handling with the following functions:
 
* File Handling with the following functions:
Line 331: Line 334:
 
'''Read''' '''Function'''
 
'''Read''' '''Function'''
  
'''mopen Function'''
+
'''mopen '''
  
'''mclose Function'''
+
'''mclose '''
  
 
|-
 
|-

Revision as of 11:59, 17 April 2013

Title of script: File Handling in Scilab

Author: Anuradha Amrutkar

Keywords: File Handling, writefile, read file, open file, close file


Visual Cue Narration
Slide Welcome to this spoken tutorial on File Handling using Scilab.
Slide In this tutorial, we will learn
  • File Handling with the following functions;
    • Write Function
    • Read Function
    • mopen Function
    • mclose Function
Slide System Requirements

For Demonstration i am using Ubuntu Linux12.04 operating system with Scilab version 5.3.3 installed


Slide Pre-requisites:

You should have

  1. Basic knowledge of Scilab.
  2. If not, for relevant spoken tutorials please visit spoken hyphen tutorial dot org



Slide Now we will see some functions in scilab which are useful for File Handling.

File Handling involves-

  • writing to a file using write function,
  • reading from a file using read function,
  • open an existing file using mopen function and
  • closing an already opened file using mclose function.


Let us start with writing data to a file.

For this purpose the write() command is used.

Scilab Console


--> random_matrix = rand(20,1)


random_matrix =


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

Switch to the scilab console window. To begin with let us create a matrix of random numbers. Type;


-->random underscore matrix is equal to rand in the bracket 20 comma 1 close the bracket semicolon and press enter



Scilab Console


--> pwd


Now check the present working directory. Type


-->pwd

ans =


/home/fossee


In my case the present working directory is slash home slash fossee

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.


Type;--> write into bracket into quotes random dash numbers dot txt close  the  quotes comma random underscore matrix close the bracket and press enter
Let us see whether this file has been created.

I will minimize the scilab console window.


And open the file which will be created and saved in the fossee directory of my computer.

You can see the data from variable random underscore matrix is written in a text file random dash numbers dot txt
Scilab Console


-->new_vector = read('random-numbers.txt', 20, 1)


new_vector =

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

I will close this file. Coming back to the scilab console. Now we will see how to read the data from a file.


For this we will use the command read as follows,


Typenew underscore vector is equal to read into bracket in to quote random dash numbers Dot txt close the quote comma 20 comma 1 close the bracket and press enter.


The read command reads all the data from the file mentioned in the argument,


In this case random dash numbers dot txt,


And stores in the variable new underscore vector Press Enter to continue the display

Narration:

-->new_vector = read('random-numbers.txt', 19, 1)

If we modify the above command as


new underscore vector is equal to read into bracket into quote random dash numbers Dot txt comma 19 comma 1


The read command reads only 19 data values from the file mentioned in this argument.


In this case random dash numbers dot txt


And stores in the variable new underscore vector.


Issue this command on the scilab console and verify the output.

Slide Now let us see about themopen function:


fd = mopen(file-name, mode)


mopen command is used to open an existing file in a way compatible to C fopen procedure.


mode is a character string that controls whether the file is opened for

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
fd underscore r is equal to mopen('random-numbers,'rt')

the above command opens the 'random-numbers' as a text and read-only mode.

Slide mclose:

mclose command closes the opened file by using mopen.

mclose(fd)


where fd is the file descriptor of the opened file


If fd is omitted mclose closes the last opened file.

Scilab Console

-->mclose(fd_r)

ans =

0.

mclose into bracket fdcloses the opened file by using mopen

Where fd is the file descriptor of the opened file.

If fd is omitted mclose closes the last opened file.

Thats all we have in this tutorial.

Summary That's all we have in this tutorial.
You have learnt - 
  • File Handling with the following functions:

Write Function

Read Function

mopen

mclose

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

contact@spoken-tutorial.org

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

Contributors and Content Editors

Lavitha Pereira, Sneha