When working with Scilab there are two ways of evaluating commands and functions. The first one is by inserting them in the console and evaluating the result. This is works fine if we want to test short pieces of code, instructions or we just want to see how a function works.
-->randomMatrix = round(rand(3,3)*10) randomMatrix = 5. 6. 0. 4. 4. 5. 3. 9. 3. -->
The problem comes when we want to assess the function for different parameters. Then we have to reload it in the console, change the parameters and evaluate it again.
Things become complicate when we have a succession of functions to evaluate. By using the console we evaluate them one by one. For example we want to multiply two 2 x 2 matrices.
First we define the matrices:
-->matrix_1 = [1 2;3 4] matrix_1 = 1. 2. 3. 4. -->matrix_2 = [5 6;7 8] matrix_2 = 5. 6. 7. 8. -->
Second we multiply them and assign the result to a third matrix:
-->matrix_3 = matrix_1 * matrix_2 matrix_3 = 19. 22. 43. 50. -->
If we want to evaluate the same operation for different value of matrix_1
or matrix_2
we have to perform the same steps again, assigning values and do the multiplication. If our algorithm contains several operations then clearly doing them at the console will not be very efficient.
A more efficient way is to put all our variables definition and operation in a text file and evaluate them at the end. To do this we open Scilab editor named SciNotes. You can do this by clicking on Applications and then SciNotes or by entering scinotes
at the console.
The Scilab editor is quite simple, it’s a text editor with a few particular features:
- it has syntax highlight: the variables, functions, comments, etc. have different colours;
- can run multiple scripts simultaneously
- can perform text indentation automatically
If we put “//”
at the beginning of each line in the script we inform Scilab that this is a comment and should not be evaluated.
After we entered all our instruction if we press F5
key the script will be evaluated and the results displayed in the console. The advantage, compared to the console, is that you can change the values of the variable or change the order of the operations, add new operation and evaluate them with a single run.
All scripts need to be saved before being evaluated. Otherwise Scilab will pop up a window asking if you want to save the script.
Script files can be saved as *.sce
or *.sci
files. It is recommended to use *.sce
because *.sci
is more related to user defined functions.
By using scripts we can develop complex algorithms and user defined (custom) function. Scilab scripts should be the default way of using Scilab and the console just for function understanding, short tests and result display.
For any questions, observations and queries regarding Scilab variables use the comment form below.
Don’t forget to Like, Share and Subscribe!