Tuesday, November 8, 2011

Scilab Script and Function Files

How is a script file different from a function file?
A script file may contain only Scilab executable statements or both executable statements and function definitions. On the other hand, a function file can contain only function definitions without any Scilab executable statements.

Whether it is a script file or a function file, it is written in a text editor (such as Notepad on Windows, vi/gedit/kate (or other similar editors) on GNU/Linux). But it is best to use SciNotes which is built-in into Scilab as it can highlight Scilab code based on syntax, auto indent where necessary and reduces typing (by completing closing brackets automatically, completing endfunction when defining a function etc.).

After your script/function file is ready, it is a good idea to save it in a file on disk (in a location that you remember so that you can retrieve it easily when you need it later), but it is not mandatory. That is, you can load a script/function file into Scilab workspace without saving it by choosing Execute -> file with echo (Ctrl+L) or Execute -> until the caret, with echo (Ctrl+E).

If the file is a script file, it will be executed immediately as if the lines in your script file were typed in the Scilab Console one by one. If there are any function definitions in a script file, they are compiled and loaded into Scilab workspace and are available for use subsequent to their definition.

If the file is a function file, the functions are compiled and loaded into Scilab workspace, and are available for subsequent use, either in the console or in other script or function files. If a file is a script file, it will be executed immediately.

Here are some rules for script/function files:
  1. One script file may contain zero or more function definitions and one or more Scilab executable statements.
  2. One function file may contain one or more function definitions and zero Scilab executable statements.
  3. Name of the script/function file can be chosen by the progarmmer to follow file naming conventions of the operating system. By default, Scilab executable files have the extension .sce. (Unlike Matlab(R), there is no requirement that one function file must contain only one function definition and name of the function file must be the same as the name of the function, with the extension .m).
  4. Scilab script files must be executed by loading it from SciNotes or by invoking the function exec().
  5. Scilab function files must be loaded into Scilab workspace before the functions can be used. This can be done by loading the file from SciNotes or using the function exec().
If you are writing a function, it is obvious that you should use SciNotes because functions are written to be used again and again, and need to go through the write-test-debug cycle. However, Scilab commands could be typed at the Scilab console and therefore do not need to be written in a script file. But what if the set of commands is large, or you are prototyping an algorithm and want to have a record of all commands you issued? In such a situation, it is advisable to use SciNotes to type the Scilab commands, save them in a file on disk, execute the code from inside SciNotes (Ctrl+L) and do the write-run-debug cycle entirely within SciNotes.

Typically, a function file contains a set of functions pertaining to one particular application area. Such a function file, along with examples, help files etc. can be made into a toolbox by following the instructions on Scilab website for developers. They can then be submitted to Scilab ATOMS for distribution to Scilab users. ATOMS is similar to Android Market or iTunes AppStore, except that this concept existed long before AppMarket became a buzzword.

No comments:

Post a Comment

Your comments are ciritical input for the sustainance and improvement of this blog.