Every Scilab script, function or routine that we need to write will contain for sure variables. From the variables point of view Scilab is very flexible, similar to Matlab®.
Scilab variables are named also objects. So in other tutorials, books, you’ll find “objects” instead of “variables”. I prefer to call them variables maybe because the term “variables” is used in other programming languages like C or Python.
In order to define a variable, in the Scilab console type: „variable name” = „value” and press <ENTER>:
-->variable_name = 12 variable_name = 12. -->
Variable naming rules
Scilab has specific rules for variable definition that must be taken into account. The variable names can contain any:
- letters, uppercase or lowercase (e.g.
- numbers, except for the first character (e.g.
- of the following characters (e.g.
* The percent character can only be used as first character in the variable name
The length of the variable name is limited to 24 characters. Any characters above 24 will be ignored.
-->_12345678910111213141516171819202122232425 = 10 Warning : The identifier : _12345678910111213141516171819202122232425 has been truncated to: _12345678910111213141516. _12345678910111213141516 = 10. -->
Be ware that the variables names are stores in the same space with function names. So if you name your variable the same as a function, Scilab will use the variable instead of function.
-->sin=89 Warning : redefining function: sin . Use funcprot(0) to avoid this message sin = 89. -->sin+18 ans = 107. -->cos+18 !--error 144 Undefined operation for the given operands. check or define function %fptr_a_s for overloading. -->clear sin -->sin+18 !--error 144 Undefined operation for the given operands. check or define function %fptr_a_s for overloading. -->
In the example above we have created a variable named
sin which will be used as a variable instead as a trigonometric function. Scilab warned me about this function redefining! After
sin declaration I can used it as an ordinary variable. This was not normally possible with a function (see example with
Even if it’s not recommended from the readability point of view, you can define a Scilab variable name as:
-->%#?!$_ = 123 %#?!$_ = 123. -->
Default coding for Scilab variables is in double precision, on 64 bits. So if you don’t specify the data type of the variable, Scilab will store it as double.
-->var1 = 3.6 var1 = 3.6 -->
Since all the variables are coded in a specific format they have an representation error. That means that the calculation done in Scilab are not exact but with a very good accuracy. Nevertheless the variable coding within Scilab is sufficient for the most of the applications.
In order to see the approximation for the example above we will change the display format of the variable to see the maximum number of digits:
-->format(25) -->var1 = 3.6 var1 = 3.6000000000000000888178 -->
You can clearly see that
3.6 is not represented exactly but approximately with a very good precision. More, if we want to check if Scilab is interpreting
3.6 we will use the following code:
-->3.6 == var1 ans = T -->
T is a Scilab Boolean variable for „true”.
Constants – Special Scilab Variables
In Scilab are defined some special variables which are called constants. The names suggest that these variables can not change their values, they are constant. In order to access these variable the character „%” must be used at the beginning of the name.
|Constant||Description / Properties||Value|
|Greek letter π, ||3.1415927|
|Euler’s constant , ||2.7182818|
|Greek letter ε||2.2⋅10-16|
|Imaginary unit||square root of -1|
|not a number||∞ – ∞|
The values of these variables can not be changed, they are protected to writing. If you try to assign a different value to any of them, Scilab will output an error message without taking into account the instruction:
-->%pi=5 !--error 13 Redefining permanent variable. -->%pi %pi = 3.1415927 -->%e=14 !--error 13 Redefining permanent variable. -->%e %e = 2.7182818 -->
These special Scilab variables are considered predefined, they can not be deleted nor saved using the save function. A part of these variables are defined in the
scilab.start file which can be found in
SCI + “/etc” folder.
Hint: Instead of
%i, the Scilab user can use
For internal purposes Scilab uses some environment variables. The variables have the name written with uppercase fonts and they can be seen using
who(); function. The check the values of these variables just type the name in the Scilab console and press <ENTER>.
|Scilab Environment Variable||Description|
|WSCI||Returns the path of Scilab installation (in Windows filename convention)|
|MSDOS||Returns true (T) if Scilab is installed on Windows machine|
|SCI||Returns the path to Scilab installation|
|SCIHOME||Returns the path where Scilab configuration files are stored|
|TMPDIR||Returns the path to the folder where Scilab stores temporary files|
If you want to see the modules that are installed within Scilab you can use this command:
-->ls(SCI + "/modules") <ENTER>
This is about it, for the first part of Scilab Variables tutorial. In the second tutorial we will discuss about complex numbers, strigs, matrices, lists and polynomials.
For any questions, observations and queries regarding Scilab variables use the comment form below.
Don’t forget to Like, Share and Subscribe!