Using Scilab, Part 3: Scripting
It would be tedious (and an invitation to mistakes) to have to type the same
instructions over and over again, expecially as the algorithm being constructed
by the user over time becomes more complex. Fortunately there is a way to write
and save scripts which can then be invoked from within the Scilab session.
Scripts are made by using a test editor, such as PFE, to create a file, which
should have a .sci extension on it. For example, suppose we wanted to automate
the loading and addition of matrices A, B, and C, shown on in the previous
section. We could create an ascii text file called, say, script1.sci containing
the lines
![](images/scilab/scilabusage3one.GIF)
We can run this script by simply choosing the File menu, then selecting
Exec... menu item and using the dialog boxes to navigate to the location of the
script1.sci file:
![](images/scilab/scilabusage3two.GIF)
Click on the desired .sci file and then press the Open button. The result
looks like the following:
![](images/scilab/scilabusage3three.GIF)
Now compare the input with the results. Comments are denoted with two forward
slashes "//" and they designate all of the text to their right to the
end of the line not be executed. The wise programmer will use the opportunity to
comment his or her code liberally (or use literate programming) and will do so
as it is being written. This will save large amounts of grief when the code is
inevitably revisited, debugged, and revised. Also note that text to be written
to the screen can be displayed using the disp() function. Output from commands
in the .sci script file can be suppressed by appending a semicolon ";"
to the end of the executable line (see the example defining input matrices A and
B). Output is ordinarily displayed if no semicolon is appended (see the example
defining output matrix C).
Finally, once a script has been executed in the current Scilab session, it is
not necessary to go to the trouble of explicitly designating it again through
the same navigation sequence described above. Instead, Scilab is using a Win32
subset of the Linux bash shell, which allows the user to easily scroll through
the list of previously used command line inputs using the up and down arrow keys
on the keyboard. For example, if I were to press the up arrow key once, the
Scilab command previously executed appears:
![](images/scilab/scilabusage3four.GIF)
waiting either to be executed again (by hitting the Return key) or edited
(using the left and right arrows to move to the spot were editing is to take
place, then using the Backspace, Insert, and Delete keys as needed). This little
bash shell capability will save you a lot of time!