1.4 - Simple SCILAB Input and Output
Enroll to start learning
You’ve not yet enrolled in this course. Please enroll for free to listen to audio lessons, classroom podcasts and take practice test.
Interactive Audio Lesson
Listen to a student-teacher conversation explaining the topic in a relatable way.
Getting Started with Variables
🔒 Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
Today, we're going to learn how to assign values to variables in SCILAB. Can anyone tell me what a variable is?
Isn't it something you can change, like in math?
Exactly! In SCILAB, we can use the `=` sign to assign a value to a variable. For example, if I write `a = 3.2`, `a` now holds the value 3.2.
What happens if I want to check what `a` holds?
Good question! You can simply type `a` and press Enter. SCILAB will display the value stored in `a`. Let's practice that!
Basic Arithmetic Operations
🔒 Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
Now that we know how to use variables, let's perform some basic arithmetic operations. What would `a + 6.4` do?
It should add 6.4 to the value of `a` right?
Correct! In SCILAB, we can do this in real-time. Let’s calculate together. What about `a - 6.4`?
That would subtract 6.4 from `a`.
That's right. Remember, just use the operators for addition, subtraction, multiplication, and division with your variables!
Using the Diary Function
🔒 Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
Let’s discuss the `diary` function. Can anyone guess what it does?
Is it used to keep a record of our SCILAB commands?
Exactly! By using `diary('session1.txt')`, we start logging all commands and outputs to a file. How do we stop logging?
I think we use `diary(0)` to stop it, right?
That's perfect! This is especially useful for documenting your work and findings in SCILAB.
Exploring Special Constants
🔒 Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
SCILAB has several special constants that you should remember. For instance, can anyone tell me what `%pi` represents?
%pi is the ratio of a circle's circumference to its diameter, right?
That's correct! And we also have `%e` for the base of natural logarithms. Let's try using these constants in an expression, like `%pi * 2`.
Will it give us the circumference of a circle of radius 1?
Exactly! Constants like these make calculations easier. Remember, knowing them is essential!
Introduction & Overview
Read summaries of the section's main ideas at different levels of detail.
Quick Overview
Standard
In this section, the reader learns about various standard input and output functions in SCILAB, how to perform simple arithmetic operations, create and manipulate variables, and the significance of using the diary function to save session outputs. Important SCILAB constants and commands are introduced as well.
Detailed
Simple SCILAB Input and Output
This section provides users with foundational knowledge about input and output functionalities in SCILAB, a powerful numerical computing environment. Users are guided through the critical steps of performing numerical calculations, managing variables, and understanding different output commands.
Key Functions and Operations
-
Variable Assignment: Variables can be assigned values using the assignment operator
=. Commands to interact with variables include typical operations (addition, subtraction, multiplication, division) and some special constants like%pifor pi,%ifor imaginary units, among others. -
Output Management: The
diaryfunction captures all output during a SCILAB session to a specified text file. Users learn how to initiate and terminate logging of commands and results effectively. -
Special Constants: Understanding key constants (
%eps, %inf, %nan, %t, %f) is essential as they are frequently utilized in calculations. - Command History: SCILAB maintains a command history that allows users to reuse commands easily, fostering a more efficient workflow.
- Basic Commands: Usage of basic print functionalities, manipulation of vectors and matrices, and executing calculations are discussed. The section emphasizes how to ensure commands execute correctly without errors, providing a solid groundwork for numerical tasks.
Youtube Videos
Audio Book
Dive deep into the subject with an immersive audiobook experience.
Output Management with Diary
Chapter 1 of 5
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
Output: To get a list of your current session use the function diary. The format is as follows: diary (output_filename), where the filename is written within quotes. To end collecting output in the current diary output file use diary(0). For example, try the following session:
(Press Restart)
diary ('session1.txt')
A = [1. 2. 3.; 2. 3. 1.; 3. 2. 1.];b=[5; 4; -1.];
A
b
x = linsolve(A,b)
diary(0)
Next, use NOTEPAD, or other text editor, to open the file session1.txt to see the contents of the file.
Detailed Explanation
The diary function in SCILAB is used to record all output from a session into a file. To start recording, you use diary('filename.txt'), replacing 'filename.txt' with your desired file name. After this, any output displayed in the SCILAB command window will also be saved in this file. You can stop recording by using diary(0). When you open the resulting text file, you can see all your commands and their outputs neatly collected, which is useful for documentation or review of your work.
Examples & Analogies
Think of the diary function as keeping a detailed journal of your experiments with SCILAB. Just like a scientist keeps notes of their observations and results in a lab notebook, you can document everything you're doing in SCILAB. This makes it easier to go back and review your results later, much like revisiting notes to prepare for an exam.
Using Script Files
Chapter 2 of 5
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
Command Input: you can read SCILAB commands from a script file, which is basically a text file listing all the commands you want to use. As an example, create the following text file in the bin subdirectory of the SCILAB directory using NOTEPAD, and call it session2.txt:
//--------------------------------------------------------------------------
clear
A = [1. 2. -3. // entering
3. 4. 5. // elements of
7. 8. 9.] // matrix A
b = [1.; 2.; 3.] // enter vector b
xa = inv(A)*b // calculate x using matrix inverse
xb = linsolve(A,b) // calculate x using SCILAB's own linsolve function
//--------------------------------------------------------------------------
Then,press Restart in SCILAB, and type: exec('session2.txt'). You will see SCILAB execute all the commands in your file, stopping at the end of the file.
Detailed Explanation
In SCILAB, you can automate tasks by saving sets of commands in script files. These script files are just plain text files that can contain any valid SCILAB code. Whenever you want to run the set of commands, you can simply type exec('filename.txt') in SCILAB, and it will execute all the commands listed in that file sequentially. This saves time and reduces the chances of errors from manually entering commands each time.
Examples & Analogies
Imagine you’re assembling a piece of IKEA furniture. Instead of trying to remember and follow the assembly steps in your head every time, you have an instruction manual that you follow. Similarly, script files act as instruction manuals for SCILAB, allowing you to run complex sequences of code without having to retype them each time.
SCILAB Command History
Chapter 3 of 5
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
SCILAB command history: All commands entered in a given SCILAB session get stored into a SCILAB command history buffer. The commands are thus accessible for re-use or edition. All the command history functions are available through the option History under the File menu in the SCILAB worksheet. The most useful commands are cntl-P and cntl-N, which lets you access the previous command or the next command, respectively, in the command history buffer. Once a command is recalled from the command history buffer it can be edited by using the backspace or delete keys, or by inserting new characters by simply typing at the proper location.
Detailed Explanation
SCILAB keeps a history of all commands you enter during your session. This allows you to easily retrieve and re-execute previous commands without needing to remember everything you typed. By using the keyboard shortcuts cntl-P to go back and cntl-N to go forward in the history, you can quickly navigate through your past inputs. You can also modify the recalled command before running it, which is a great way to correct mistakes or make slight adjustments without retyping everything.
Examples & Analogies
It’s like having a conversation transcript on your phone; you can scroll through previous messages to find any specific one. If you want to resend a message but tweak it a little, you can easily do that instead of trying to remember the exact wording. This makes working in SCILAB faster and more efficient.
Selective Output in the Command History
Chapter 4 of 5
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
Selective worksheet output: Suppose you have been working on a lengthy SCILAB session whose command history buffer contains some results that produced errors as well as some intermediary results that are not of interest to you for output. For your output, you can select only those commands relevant to your final results by using Cntl-P and Cntl-N. Try the following SCILAB session that explores some basic vector operations using vectors x and y:
1 - Press the Restart option in the menu bar.
2 - Enter the following SCILAB commands:
-->x = [1, 2, 5, -4]
x =
! 1. 2. 5. - 4. !
-->y = [0, 2, 3,-5]
y =
! 0. 2. 3. - 5. !
-->xy
!--error 10
inconsistent multiplication
-->x.y
ans =
! 0. 4. 15. 20. !
... -->diary(0) The session, except for the very first command () is stored in file c:\myVectors.txt. This file can be edited or printed from a text editor such as NOTEPAD, or my favorite, PFE (Programmer’s File Editor) available for free from: http://www.lancs.ac.uk/people/cpaap/pfe.
Detailed Explanation
In a productive SCILAB session, you might execute numerous commands, some of which are not useful for your final output or analysis. You can streamline your documentation by selectively recording only the commands that yield significant results or insights. After running your commands, you can use the diary function to create a text file containing just the relevant commands and outputs. This way, you avoid clutter in your output files, allowing for easier analysis later.
Examples & Analogies
Think of it like cutting out the important parts of a newspaper article and discarding the rest. When reporting or sharing your findings, it’s best to focus on the key information that your audience will find useful, rather than overwhelming them with every little detail.
Managing SCILAB Working Directory
Chapter 5 of 5
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
Current directory / creating a work directory: SCILAB uses a current directory where files are saved by default, for example when using the function diary. To see the current directory use:
-->pwd
Under a Windows operating system, the default current directory is typically c:\
The command pwd stands for print working directory.
I recommend that you create a sub-directory, or folder, called work and locate it under the SCILAB main directory. For example, under a Windows operating system, the SCILAB main directory will typically be c:\Program Files\SCILAB2.5
Thus, your work directory would correspond to:
c:\Program Files\SCILAB2.5\work
At the beginning of a SCILAB session, you can change the current directory to the work directory by using the function chdir:
--> chdir(‘c:\Program Files\SCILAB2.5\work’)
You can use your work directory to store scripts and functions that you create.
Detailed Explanation
In SCILAB, the working directory is the location on your computer where files, including scripts and output files, are saved by default. To find out what directory you’re currently in, use the command pwd, which tells you 'print working directory'. It’s helpful to set up a dedicated folder (like 'work') within your SCILAB installation to keep your related files organized. You can change your working directory to this folder using the chdir command, which simplifies file management when working on different projects.
Examples & Analogies
Imagine working in a lab where all your tools and experiments are scattered everywhere. It would be chaotic! By creating a specific workspace or lab bench where everything you might need is within reach, you make your work much more efficient and organized. Similarly, setting a work directory in SCILAB keeps everything related to your current project neatly filed away.
Key Concepts
-
Variable Assignment: Use '=' to assign values.
-
Arithmetic Operations: Includes addition, subtraction, multiplication, and division.
-
Diary Function: Logs input/output of a SCILAB session to a file.
-
Special Constants: Includes %pi, %i, %e, and other predefined constants.
Examples & Applications
Assigning a value: a = 3.2.
Logging session output: diary('session1.txt') and diary(0).
Using constants: %pi, %i, calculating %pi * 2.
Memory Aids
Interactive tools to help you remember key concepts
Rhymes
In SCILAB, a variable is key, Assign it a value, easy as can be!
Stories
Imagine a clever mathematician named SCILAB who records all their adventures, capturing numbers and operations into a magic diary, allowing them to revisit their calculations with a simple command.
Memory Tools
Remember 'P.I.E.' for 'Pi is Essential' in SCILAB for calculations and constants.
Acronyms
SCILAB
S(C)ientific I(nput) and L(ogging) A(rithmetics and B)asic functions.
Flash Cards
Glossary
- Variable
A symbol used to represent a value that can be changed.
- Diary Function
A function used to log all input and output commands executed during a SCILAB session into a text file.
- %pi
The mathematical constant representing the ratio of a circle's circumference to its diameter, approximately 3.14.
- %i
The unit imaginary number in SCILAB.
- %e
The base of natural logarithms, approximately equal to 2.718.
Reference links
Supplementary resources to enhance your learning experience.