Difference between revisions of "Scilab/C4/User-Defined-Input-and-Output/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script''': User defined Input/output in Scilab '''Author: Anuradha Amrutkar''' '''Keywords: input, mprintf, save() , load()''' {| border=1 !Visual Cue !Narration…')
 
 
(4 intermediate revisions by 2 users not shown)
Line 4: Line 4:
  
 
'''Keywords: input, mprintf, save() , load()'''
 
'''Keywords: input, mprintf, save() , load()'''
 
  
  
Line 11: Line 10:
 
!Narration
 
!Narration
 
|-
 
|-
 +
  
 
|-
 
|-
Line 25: Line 25:
 
* '''load''' function
 
* '''load''' 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 Ubuntu Linux12.04 operating system  with Scilab version 5.3.3 installed
  
  
Line 34: Line 39:
  
 
# 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 on scilab 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:
  
 
* is used to take the input from the user.
 
* is used to take the input from the user.
* provides a prompt in the text string for user input.
+
*It provides a prompt in the text string for user input.
* waits for input from the keyboard.
+
* It waits for input from the keyboard.
  
 
If nothing but a carriage return is entered at the prompt, '''input()''' function returns an empty matrix.
 
If nothing but a carriage return is entered at the prompt, '''input()''' function returns an empty matrix.
Line 58: Line 58:
 
|-
 
|-
 
| 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;"| The '''input''' function can be written in two ways as you see.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The '''input''' function can be written in two ways .
 
+
First ,x= input(''message to display'')
 
+
Second,x= input("message to display","strings")
 
In the second example, the second argument is “'''string'''”.
 
In the second example, the second argument is “'''string'''”.
  
Line 73: Line 73:
  
 
--> y = input (“ Enter your age”, “string”)
 
--> y = input (“ Enter your age”, “string”)
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type the following on the''' Scilab Console'''
+
| 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 and type,
 +
 +
'''-->x is equal to input open bracket inside double quotes Enter your age close the double quotes close the bracket''' and press enter.Type 25 and press enter
  
'''-->x is equal to input open bracket inside double quotes Enter your age colon close the bracket'''
 
  
 
I will enter a number say 25 here.
 
 
Its shows the output as
 
 
'''x <nowiki>=</nowiki>'''
 
 
 
'''25.'''
 
  
  
 
Now type
 
Now type
  
'''-->y is equal to input open bracket inside double quotes Enter your age comma inside double quotes string close the bracket.'''
+
'''-->y is equal to input into bracket into double quotes Enter your age close the double quotes comma again inside double quotes write string close the bracket.''' and press enter
 
+
 
+
I will enter a number say 25 here.
+
  
Its shows the output
 
  
'''y <nowiki>=</nowiki>'''
+
type 25 and press enter
  
  
'''25 '''
 
  
 
|-
 
|-
 
| 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;"| We see that in both the cases the input we entered, was a number 25.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see that in both the cases the input we entered through the keyboard, was a number 25.
  
 
|-
 
|-
Line 110: Line 97:
 
| 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 check the type of variable that '''x''' and '''y''' are.
 
| 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 check the type of variable that '''x''' and '''y''' are.
  
 
+
Let us clear the console using  clc command.
 
We are doing this to validate the use and importance of the argument “string”, given in the second example.  
 
We are doing this to validate the use and importance of the argument “string”, given in the second example.  
  
Line 121: Line 108:
  
  
'''-->typeof(x)'''
+
'''-->typeof(x)'''and press enter
 
+
'''ans is equal to '''
+
 
+
 
+
'''constant '''
+
  
  
'''-->typeof(y)'''
+
similarly;
  
'''ans <nowiki>=</nowiki>'''
+
'''-->typeof(y)''' and press enter
  
  
'''string '''
 
  
 
|-
 
|-
Line 160: Line 141:
 
|-
 
|-
 
| 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;"| Let us see an example for this.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see an example for this.Switch to the console
  
Type the following on the '''Scilab Console'''
 
  
 
|-
 
|-
Line 174: Line 154:
  
 
'''is taken as a STRING'''
 
'''is taken as a STRING'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''-->mprintf open bracket open double quotes Value of x is equal to percentage d is taken as a CONSTANT comma while value of y is equal to percentage s is taken as a STRING close double quotes comma x comma y close the bracket'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type'''-->mprintf into bracket into quotes type iteration    percent i comma result is colon slash n alpha equal to percentf  comma 33 comma 0.535 close the  bracket, Here 33 will be displayed in place of  percent i and point535 will be displayed in place of percent f as a flow, press enter
 
+
This will give the output as '' At iteration 33, Result is alpha is equal to 0.535000. Clear the console. Now let us see another example.
 +
In print f open bracket  into quotes value of x is equal to percentage d is taken as a constant comma while value of y is equal to percent s is taken as a string close the quotes comma x comma y close the bracket
  
 
In the above example  
 
In the above example  
  
 
* '''percentage d''' is used to insert an '''constant''' data stored in variable '''x''' and  
 
* '''percentage d''' is used to insert an '''constant''' data stored in variable '''x''' and  
* '''percentage s '''is used to insert a '''string''' data stored in variable '''y.'''
+
* '''percentage s '''is used to insert a '''string''' data stored in variable '''y.''' press enter you see the output
  
  
Line 191: Line 172:
 
To quit '''Scilab''' midway through a calcula-  
 
To quit '''Scilab''' midway through a calcula-  
  
tion and  
+
tion and to
  
continue at a later stage type  
+
continue to a later stage type  
  
  
'''--> save thissession'''
+
'''--> save this session'''
  
  
Line 213: Line 194:
  
  
'''--> load thissession'''  
+
'''--> load this session'''  
  
 
and the computation can be resumed where you left off.  
 
and the computation can be resumed where you left off.  
Line 220: Line 201:
 
The purpose of '''save''' and '''load''' function are  
 
The purpose of '''save''' and '''load''' function are  
  
* The '''save() '''command saves the '''scilab''' current variables in a binary file.
+
* The '''save() '''command saves all the '''scilab''' current variables in a binary file.
 
* If the variable is a graphic handle, the '''save''' function saves all the corresponding graphics_entities definition.
 
* If the variable is a graphic handle, the '''save''' function saves all the corresponding graphics_entities definition.
 
* The file can be given either by its paths or by its descriptor previously given.
 
* The file can be given either by its paths or by its descriptor previously given.
Line 228: Line 209:
 
|-
 
|-
 
| 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;"| * <tt>'''save(filename)'''</tt> saves all current variables in the file defined by <tt>filename</tt>.  
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| * <tt>'''save(filename)'''</tt> saves all the current variables in a file defined by <tt>filename</tt>.  
* <tt>'''save(fd)'''</tt> saves all current variables in the file defined by the descriptor <tt>fd</tt>.  
+
* <tt>'''save(fd)'''</tt> saves all the current variables in the file defined by the descriptor <tt>fd</tt>.  
 
* <tt>'''save(filename,x,y)'''</tt> or <tt>'''save(fd,x,y)'''</tt> saves only named variables <tt>x</tt> and <tt>y</tt>.  
 
* <tt>'''save(filename,x,y)'''</tt> or <tt>'''save(fd,x,y)'''</tt> saves only named variables <tt>x</tt> and <tt>y</tt>.  
  
Line 258: Line 239:
 
--> save('matrix-a-b.dat', a, b)
 
--> save('matrix-a-b.dat', a, b)
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see an example to illustrate the '''save''' and '''load''' commands usage.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see an example to illustrate the '''save''' and '''load''' commands usage.
 
+
Switch back to the console .
 
Let us define two matrices say '''a''' and '''b'''
 
Let us define two matrices say '''a''' and '''b'''
  
  
-'''->a = eye(2,2)'''
+
-'''->a = eye(2,2)''' and press enter
 
+
  
'''-->b=ones(a)'''
+
Type;
 +
'''-->b=ones(a)'''and press enter
  
 +
Clear the console using clc command. Now type
  
 
'''--> save matrix-a-b '''
 
'''--> save matrix-a-b '''
Line 272: Line 254:
 
or it can also be written as  
 
or it can also be written as  
  
-'''->save('matrix-a-b.dat',a,b)'''
+
-'''->save('matrix-a-b.dat',a,b)'''and press enter
 +
 
  
  
Line 278: Line 261:
  
  
You can browse the present working directory to check the existence of this binary file.
+
You can browse the present working directory to check the existence of this binary file. You can see it here. I will close the file browser.
  
 
|-
 
|-
Line 290: Line 273:
 
| 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 load the file back in to the variables.
 
| 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 load the file back in to the variables.
  
Before this, let us clear the variables '''a '''and '''b'''
+
Before this, let us clear the variables '''a '''and '''b'''Type
 +
 
  
 +
'''-->clear a space b'''and press enter
  
'''-->clear a space b'''
 
  
  
Line 320: Line 304:
  
  
| 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 load back the values from the binary files in these variables '''a''' and '''b''' using the '''load''' command.
+
| 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 load back the values from the binary files in these variables '''a''' and '''b''' using the '''load''' command. Type
  
  
'''Load open bracket open single quote matrix dash a dash b dot dat close quotes comma inside quotes a comma inside quotes b close the bracket'''
+
'''Load into  bracket into  quote matrix dash a dash b dot dat close the quotes comma into quotes a comma into quotes b close the bracket'''and press enter
 +
 
  
 
|-
 
|-
Line 343: Line 328:
  
 
1. 1.  
 
1. 1.  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us check the values in variables '''a '''and''' b'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us check the values in variables '''a '''and''' b'''. Clear the console. Type
  
  
 
'''-->a'''
 
'''-->a'''
 
+
And
  
 
'''-->b'''
 
'''-->b'''
Line 356: Line 341:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Summary
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Summary
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us summarize what we learnt -  
+
| 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 learnt -  
  
 
* Input Function using '''input''' command
 
* Input Function using '''input''' command

Latest revision as of 10:46, 18 April 2013

Title of script: User defined Input/output in Scilab

Author: Anuradha Amrutkar

Keywords: input, mprintf, save() , load()


Visual Cue Narration
Slide Welcome to this spoken tutorial on User-defined Input and Output using Scilab.
Slide In this tutorial, we will learn
  • Input Function
  • Formatting the Output
  • save function
  • load function
Slide System Requirements

For Demonstration:I am 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 on scilab please visit spoken hyphen tutorial dot org



Slide Input Function:

The input() function

  • is used to take the input from the user.
  • It provides a prompt in the text string for user input.
  • It waits for input from the keyboard.

If nothing but a carriage return is entered at the prompt, input() function returns an empty matrix.

Slide The input function can be written in two ways .

First ,x= input(message to display) Second,x= input("message to display","strings") In the second example, the second argument is “string”.


So the output is a character string, which is the expression entered using the keyboard.

Scilab Console

--> x = input (“ Enter your age”)


--> y = input (“ Enter your age”, “string”)

Switch to the Scilab Console window and type,

-->x is equal to input open bracket inside double quotes Enter your age close the double quotes close the bracket and press enter.Type 25 and press enter



Now type

-->y is equal to input into bracket into double quotes Enter your age close the double quotes comma again inside double quotes write string close the bracket. and press enter


type 25 and press enter


We see that in both the cases the input we entered through the keyboard, was a number 25.
Now, let us check the type of variable that x and y are.

Let us clear the console using clc command. We are doing this to validate the use and importance of the argument “string”, given in the second example.

-->typeof(x)


-->typeof(y)

To check the type of variable, let us type


-->typeof(x)and press enter


similarly;

-->typeof(y) and press enter


You can see it yourself, that
  • the first answer stored in x is of type constant and
  • second answer stored in y, with the argument “string” included in the command, is of type string.


Slide Let us now see how to format the output that is displayed on the console.


This can be done using the mprintf() function.


mprintf() function converts, formats and writes data on to the Scilab console.


It is an interface for C-coded version of printf function.

Let us see an example for this.Switch to the console


Scilab console


-->mprintf("Value of x=%d is taken as a CONSTANT, while value of y=%s is taken as a STRING", x , y)


Value of x=25 is taken as a CONSTANT,while value of y=25

is taken as a STRING

Type-->mprintf into bracket into quotes type iteration percent i comma result is colon slash n alpha equal to percentf comma 33 comma 0.535 close the bracket, Here 33 will be displayed in place of percent i and point535 will be displayed in place of percent f as a flow, press enter
This will give the output as  At iteration 33, Result is alpha is equal to 0.535000. Clear the console. Now let us see another example.

In print f open bracket into quotes value of x is equal to percentage d is taken as a constant comma while value of y is equal to percent s is taken as a string close the quotes comma x comma y close the bracket

In the above example

  • percentage d is used to insert an constant data stored in variable x and
  • percentage s is used to insert a string data stored in variable y. press enter you see the output


Slide Now, let us discuss the use of save and load command.


To quit Scilab midway through a calcula-

tion and to

continue to a later stage type


--> save this session


This will save the current values of all variables to a file

called thissession.


This file cannot be edited.


It is in binary format.


When you next start Scilab, type


--> load this session

and the computation can be resumed where you left off.


The purpose of save and load function are

  • The save() command saves all the scilab current variables in a binary file.
  • If the variable is a graphic handle, the save function saves all the corresponding graphics_entities definition.
  • The file can be given either by its paths or by its descriptor previously given.


Slide * save(filename) saves all the current variables in a file defined by filename.
  • save(fd) saves all the current variables in the file defined by the descriptor fd.
  • save(filename,x,y) or save(fd,x,y) saves only named variables x and y.


Scilab Console


--> a = eye(2,2)

a =


1. 0.

0. 1.

--> b = ones (a)

b =


1. 1.

1. 1.

--> save('matrix-a-b.dat', a, b)

Let us see an example to illustrate the save and load commands usage.

Switch back to the console . Let us define two matrices say a and b


-->a = eye(2,2) and press enter

Type;

-->b=ones(a)and press enter

Clear the console using clc command. Now type

--> save matrix-a-b

or it can also be written as

-->save('matrix-a-b.dat',a,b)and press enter


This saves the values of variables in a binary file matrix-a-b.dat in the present working directory.


You can browse the present working directory to check the existence of this binary file. You can see it here. I will close the file browser.

Scilab Console


-->clear a b


Now let us load the file back in to the variables.

Before this, let us clear the variables a and bType


-->clear a space band press enter


Let us cross check if these variables are really cleared.


-->a

!--error 4

Undefined variable: a


-->b

!--error 4

Undefined variable: b

Scilab Console


--> load('matrix-a-b.dat', 'a', 'b')


Now let us load back the values from the binary files in these variables a and b using the load command. Type


Load into bracket into quote matrix dash a dash b dot dat close the quotes comma into quotes a comma into quotes b close the bracketand press enter


--> a

a =

1. 0.

0. 1.


--> b

b =


1. 1.

1. 1.

Let us check the values in variables a and b. Clear the console. Type


-->a

And

-->b


You can see the values are loaded back in the variables.

Summary In this tutorial we learnt -
  • Input Function using input command
  • Formatting the Output using mprintf command
  • Save Function
  • Load 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

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