-->SCI // the directory where Scilab is located
SCI =
/usr/lib/scilab-2.6
-->pwd // the directory where you read and write files
ans =
/home/j
-->chdir ('..') // moves to upper directory
ans =
0.
-->pwd // directory after movement
ans =
/home
-->chdir ('j') // returns to previous directory j
ans =
0.
Sometimes we have to access the commands of the shell
-->unix_w('mkdir test') // makes directory test
-->chdir('test') // goes to directory test
ans =
0.
-->unix_w('ls -l') // lists its content
total 0
It is simple to create a directory and to enter into it. Now we can initiate our editor and generate `helloworld.sce' with the following code. (We have to take care of saving it at the directory we created `/home/j/test').
message = 'hello world'
Now execute the next command at the Scilab prompt
-->exec('/home/j/test/helloworld.sce')
-->
If nothing happens, it may be due to the fact that you did not insert a `carriage return' at the end of the file (this fact was corrected in version 2.6). We must insert it from the editor and you will see
-->exec('/home/j/test/helloworld.sce')
-->message = 'hello world'
message =
hello world
We only introduced the first line ('exec(...'), the remainder lines were generated by Scilab. Now we can type
-->message message = hello world
The command `exec(file)' acts as if the content of `file' would have been introduced at the prompt of Scilab. The extension `sce' is not necessary but it allows to distinguish this kind of files from other files also used by Scilab. We can name `file' with its absolute address or relatively to the local directory (which we can know with `pwd').
This type of programming has some disadvantages. As we have seen, all the variables that are defined in the file are maintained afterwards. We try to keep our code as simple as possible and we like to think about only a few variables simultaneously. It would be better than the variables employed in the file did not remain active once the file has been executed. On the other side, if you put together different methods in the file the objectives of the file can be obscure. Finally since we do not define the inputs of the file, we do not know -unless we read the code- which variables are influencing the result. These facts are not very important to build small procedures but grow to be vital in large programs.
Usually, `sce' files are used to maintain the main course of the program but we need some form of encapsulation of our code in such a way that the inputs and outputs are well defined and the variables do not persist after its execution. In other words, we need `functions' to program nearly any type of program.