2 First steps
In this section, we make our ﬁrst steps with Scilab and present some simple tasks we can perform with the interpreter. There are several ways of using Scilab and the following paragraphs present three methods: • using the console in the interactive mode, • using the exec function against a ﬁle, • using batch processing. We also present the management of the graphical windows with the docking system. Finally, we present two major features of Scilab: the localization of Scilab, which provides messages and help pages in the language of the user, and the ATOMS system, a packaging system for external modules.
2.1 The console
The ﬁrst way is to use Scilab interactively, by typing commands in the console, analyzing the results and continuing this process until the ﬁnal result is computed. This document is designed so that the Scilab examples which are printed here can be copied into the console. The goal is that the reader can experiment with Scilab behavior by himself. This is indeed a good way of understanding the behavior of the program and, most of the time, it is a quick and smooth way of performing the desired computation.
In the following example, the function disp is used in the interactive mode to print out the string ”Hello World!”.
--> s="Hello World!" s = Hello World! --> disp(s) Hello World!
In the previous session, we did not type the characters ”–>”which is the prompt, and which is managed by Scilab. We only type the statement s=”Hello World!” with our keyboard and then hit the <Enter> key. Scilab answer is s = and Hello World!. Then we type disp(s) and Scilab answer is Hello World!.
When we edit a command, we can use the keyboard, as with a regular editor. We can use the left ← and right → arrows in order to move the cursor on the line and use the <Backspace> and <Suppr> keys in order to ﬁx errors in the text.
In order to get access to previously executed commands, we use the up arrow ↑ key. This lets us browse the previous commands by using the up ↑ and down ↓ arrow keys.
The <Tab> key provides a very convenient completion feature. In the following session, we type the statement disp in the console.
Then we can type on the <Tab> key, which makes a list appear in the console, as presented in ﬁgure 4. Scilab displays a listbox, where items correspond to all functions which begin with the letters ”disp”. We can then use the up and down arrow keys to select the function we want.
Figure 4: The completion in the console.
The auto-completion works with functions, variables, ﬁles and graphic handles and makes the development of scripts easier and faster.
2.2 The editor
Scilab provides an editor for editing scripts easily. Figure 5 presents the editor during the editing of the previous ”Hello World!” example.
Figure 5: The editor.
The editor can be accessed from the menu of the console, under the Applications > Editor menu, or from the console, as presented in the following session.
This editor manages several ﬁles at the same time, as presented in ﬁgure 5, where we edit ﬁve ﬁles at the same time. There are many features which are worth mentioning in this editor. The most commonly used features are under the Execute menu.
• Load into Scilab executes the statements in the current ﬁle, as if we did a copy and paste. This implies that the statements which do not end with the semicolon ”;”character will produce an output in the console.
• Evaluate Selection executes the statements which are currently selected.
• Execute File Into Scilab executes the ﬁle, as if we used the exec function. The results which are produced in the console are only those which are associated with printing functions, such as disp for example.
We can also select a few lines in the script, right click (or Cmd+Click under Mac), and get the context menu which is presented in ﬁgure 6.
Figure 6: Context menu in the editor.
The Edit menu provides a very interesting feature, commonly known as a ”pretty printer” in most languages. This is the Edit > Correct Indentation feature, which automatically indents the current selection. This feature is extremely convenient, because it formats algorithms, so that the if, for and other structured blocks are easy to analyze.
Figure 7: Context help in the editor.
The editor provides a fast access to the inline help. Indeed, assume that we have selected the disp statement, as presented in ﬁgure 7. When we right-click in the editor, we get the context menu, where the Help about ”disp” entry opens the help page associated with the disp function.
The graphics in Scilab version 5 has been updated so that many components are now based on Java. This has a number of advantages, including the possibility to manage docking windows.
The docking system uses Flexdock , an open-source project providing a Swing docking framework. Assume that we have both the console and the editor opened in our environment, as presented in ﬁgure 8. It might be annoying to manage two windows, because one may hide the other, so that we constantly have to move them around in order to actually see what happens.
Figure 8: The title bar in the source window. In order to dock the editor into the console, drag and drop the title bar of the editor into the console.
The Flexdock system allows us to drag and drop the editor into the console, so that we ﬁnally have only one window, with several sub-windows. All Scilab windows are dockable, including the console, the editor, the variable browser, the command history, the help and the plotting windows. In ﬁgure 9, we present a situation where we have docked four windows into the console window.
In order to dock one window into another window, we must drag and drop the source window into the target window. To do this, we left-click on the title bar of the docking window, as indicated in ﬁgure 8. Before releasing the click, let us move the mouse over the target window and notice that a window, surrounded by dotted lines is displayed. This ”phantom” window indicates the location of the future docked window.
We can choose this location, which can be on the top, the bottom, the left or the right of the target window. Once we have chosen the target location, we release the click, which ﬁnally moves the source window into the target window, as in ﬁgure 9.
Figure 9: Actions in the title bar of the docking window. The round arrow in the title bar of the window undocks the window. The cross closes the window.
We can also release the source window over the target window, which creates tabs, as in ﬁgure 10.
Figure 10: Docking tabs.
2.4 The variable browser and the command history
Scilab provides a variable browser, which displays the list of variables currently used in the environment. Figure 11 presents the state of this browser during a session.
Figure 11: The variable browser.
We can access this browser through the menu Applications > Variable Browser, but the function browsevar() has the same eﬀect.
We can double click on a variable, which opens the variable editor, as presented in the ﬁgure 12. We can then interactively change the value of a variable by changing its content in a cell. On the other hand, if we change the variable content within the console, we must refresh the content of the dialog box by pushing the refresh button in the toolbar of the Variable Editor.
Figure 12: The variable editor.
The Command History dialog allows browsing through the commands that we have previously executed. This dialog is available in the menu Applications > Command History and is presented in the ﬁgure 13.
Figure 13: The command history.
We can select any command in the list and double-click on it to execute it in the console. The right-click opens a context menu which lets us evaluate the command or edit it in the editor.
2.5 Using exec
When several commands are to be executed, it may be more convenient to write these statements into a ﬁle with Scilab editor. To execute the commands located in such a ﬁle, the exec function can be used, followed by the name of the script. This ﬁle generally has the extension .sce or .sci, depending on its content:
• ﬁles having the .sci extension contain Scilab functions and executing them loads the functions into Scilab environment (but does not execute them),
• ﬁles having the .sce extension contain both Scilab functions and executable statements.
Executing a .sce ﬁle has generally an eﬀect such as computing several variables and displaying the results in the console, creating 2D plots, reading or writing into a ﬁle, etc… Assume that the content of the ﬁle myscript.sce is the following.
disp("Hello World !")
In the Scilab console, we can use the exec function to execute the content of this script.
--> exec("myscript.sce") --> disp("Hello World !") Hello World !
In practical situations, such as debugging a complicated algorithm, the interactive mode is used most of the time with a sequence of calls to the exec and disp functions.
2.6 Batch processing
Another way of using Scilab is from the command line. Several command line options are available and are presented in ﬁgure 14. Whatever the operating system is, binaries are located in the directory scilab-5.3.1/bin. Command line options must be appended to the binary for the speciﬁc platform, as described below. The -nw option disables the display of the console. The -nwni option launches the nongraphics mode: in this mode, the console is not displayed and plotting functions are disabled (using them will generate an error).
• Under Windows, two binary executable are provided. The ﬁrst executable is WScilex.exe, the usual, graphics, interactive console. This executable corresponds to the icon which is available on the desktop after the installation of Scilab. The second executable is Scilex.exe, the non-graphics console. With the Scilex.exe executable, the Java-based console is not loaded and the Windows terminal is directly used. The Scilex.exe program is sensitive to the -nw and -nwni options.
• Under Linux, the scilab script provides options which conﬁgure its behavior. By default, the graphics mode is launched. The scilab script is sensitive to the -nw and -nwni options. There are two extra executables on Linux: scilab-cli and scilab-adv-cli. The scilab-adv-cli executable is equivalent to the -nw option, while the scilab-cli is equivalent to the -nwni option.
• Under Mac OS, the behavior is similar to the Linux platform.
In the following Windows session, we launch the Scilex.exe program with the -nwni option. Then we run the plot function in order to check that this function is not available in the non-graphics mode.
C:\Program Files\scilab-5.5.2\bin>Scilex.exe -nwni Scilab 5.5.2 (Mar 31 2015, 12:04:21) -->plot() !--error 4 Undefined variable: plot
|-e instruction||execute the Scilab instruction given in instruction|
|-f ﬁle||execute the Scilab script given in the ﬁle|
|-l lang||setup the user language for example, ”fr”for french and ”en”for english (default is ”en”)|
|-mem N||set the initial stacksize|
|-ns||if this option is present, the startup ﬁle scilab.start is not executed|
|-nb||if this option is present, then Scilab welcome banner is not displayed|
|-nouserstartup||don’t execute user startup ﬁles SCIHOME/.scilab or SCIHOME/scilab.ini|
|-nw||start Scilab as command line with advanced features (e.g., graphics)|
|-nwni||start Scilab as command line without advanced features|
|-version||print product version and exit|
Figure 14: Scilab command line options.
The most useful command line option is the -f option, which executes the commands from a given ﬁle, a method generally called batch processing. Assume that the content of the ﬁle myscript2.sce is the following, where the quit function is used to exit from Scilab.
disp("Hello World !") quit()
The default behavior of Scilab is to wait for new user input: this is why the quit command is used, so that the session terminates. To execute the demonstration under Windows, we created the directory ”C:\scripts” and wrote the statements in the ﬁle C:\scripts\helloworld.sce. The following session, executed from the MS Windows terminal, shows how to use the -f option to execute the previous script. Notice that we used the absolute path of the Scilex.exe executable.
C:\scripts>"C:\Program Files\scilab-5.5.2\bin\Scilex.exe" -f helloworld.sce Scilab 5.5.2 (Mar 31 2015, 12:04:21) Hello World!
Any line which begins with the two slash characters ”//”is considered by Scilab as a comment and is ignored. To check that Scilab stays by default in interactive mode, we comment out the quit statement with the ”//”syntax, as in the following script.
disp("Hello World !") //quit()
By default, Scilab provides its messages and its help pages in the English language. But it can also provide them in French, in Chinese, in Portuguese and in several other languages. In this section, we review these features and see their eﬀect in Scilab.
The localization features of Scilab change two diﬀerent set of features in Scilab:
• the messages of the Scilab application (menus, error messages, etc…),
• help pages.
The table 15 presents the list of languages supported by Scilab 5.5.2 for the application itself. For some of these languages, the help pages of Scilab are (partially) translated, as indicated in the table.
|ca_ES||Catalan – Spain|
|de_DE||German – Germany|
|en_US||English – United States|
|es_ES||Spanish – Castilian – Spain|
|fr_FR||French – France (with help pages)|
|it_IT||Italian – Italy|
|ja_JP||Japanese – Japan (with help pages)|
|pl_PL||Polish – Poland|
|pt_BR||Portuguese – Brazil (with help pages)|
|ru_RU||Russian – Russian Federation|
|uk_UA||Ukrainian – Ukraine|
Figure 15: Languages supported by Scilab.
Scilab provides several functions which manages the localization. These functions are presented in the table 16.
|getdefaultlanguage||Returns the default language used by Scilab.|
|getlanguage||Returns current language used by Scilab.|
|setdefaultlanguage||Sets and saves the internal LANGUAGE value.|
|setlanguage||Sets the internal LANGUAGE value.|
|dgettext||Get text translated into the current locale and a speciﬁc domain.|
|gettext||Get text translated into the current locale and domain.|
|_||Get text translated into the current locale and domain.|
Figure 16: Functions related to localization.
On Windows, we can use the setdefaultlanguage function, which takes a string representing the required language as input argument. Then we restart Scilab so that the menus of the console are translated. In the following example, we use the setdefaultlanguage function in order to conﬁgure the language in Portuguese.
When we restart Scilab, the error messages are provided in Portuguese:
-->1+"foo" !--error 144 Opera~A˘g~Aˇco indefinida para os dados operandos. Verifique ou defina a fun~A˘g~Aˇco %s_a_c para overloading.
The ﬁgure 17 presents the help page of the bitand function in Japanese.
Under GNU/Linux, Scilab uses the language of the operating system, so that most users should get Scilab in their own language without conﬁguring Scilab. For example, in Ubuntu, installing and conﬁguring languages can be done in the System > Administration > Language Support menu.
Under GNU/Linux or Mac OS X, an other way to start Scilab in an other language is to set the LANG environment variable. For example, the following command in the Linux terminal runs Scilab in Japanese:
# Starts Scilab in Japanese LANG=ja_JP scilab
Still, there might be diﬀerences between the language provided by GNU/Linux, and the language used by Scilab. These diﬀerences may come from a wrong deﬁnition of the language, where Scilab cannot ﬁnd the language which corresponds to the expected one. When we run Scilab from a Linux terminal, the following message may appear:
$ Warning: Localization issue. Does not support the locale ’’ (null) C. (process:1516): Gtk-WARNING **: Locale not supported by C library. Using the fallback ’C’ locale.
One common reason for this error is the various ways to deﬁne a language. Notice, for example, that there is a diﬀerence between the ”fr” language (French) and the ”fr FR” language (French in France). In this case, we must conﬁgure the language to ”fr FR”, which is the language detected by Scilab. Another reason, especially on the Debian GNU/Linux distribution, is that the locale might not have been compiled. In this case, we can use the ”dpkg-reconﬁgure locales” command in the Linux terminal.
2.8 ATOMS, the packaging system of Scilab
In this section, we present ATOMS, which is a set of tools designed to install prebuilt toolboxes.
Scilab is designed to be extended by users, who can create new functions and use them as if they were distributed with Scilab. These extensions are called ”toolboxes” or ”external modules”. The creation of a new module, with its associated help pages and unit tests, is relatively simple and this is a part of the success of Scilab.
However, most modules cannot be used directly in source form: the module has to be compiled so that the binary ﬁles can be loaded into Scilab. This compilation step is not straightforward and may be even impossible for users who want to use a module based on C or Fortran source code and who do not have a compiler. This is one of the issues that ATOMS has solved: modules are provided in binary form, allowing the user to install a module without any compilation phase and without Scilab compabilities issues.
An extra feature for the user is that most modules are available on all platforms: the developper beneﬁts from the compilation farm of the Scilab Consortium, and the user beneﬁts from a module which is, most of the time, guaranteed to be crossplatform.
ATOMS is the packaging system of Scilab external modules. With this tool, prebuilt (i.e. pre-compiled) Scilab modules, can be downloaded, installed and loaded. The dependencies are managed, so that if a module A depends on a module B, the installation of the module A automatically installs the module B. This is similar to the packaging system available in most GNU/Linux/BSD distributions. ATOMS modules are available on all operating systems on which Scilab is available, that is, on Microsoft Windows, GNU/Linux and Mac OS X. For example, when a ATOMS module is installed on Scilab running on a MS Windows operating system, the pre-built module corresponding to the MS Windows version of the module is automatically installed. The web portal for ATOMS is:
This portal presents the complete list of ATOMS modules and let the developpers of the modules upload their new modules. The ﬁgure 18 presents the 10 most downloaded ATOMS modules. This is just an arbitrary set of modules: more than 100 modules are currently available on ATOMS.
|Image Processing Design Toolbox (“IPD”)
Functions for object detection
Scilab Image and Video Processing toolbox (“SIVP”)
Image and video processing
Dynamic link with MinGW for Scilab on Windows
Check input arguments in macros
CPGE dedicated Xcos blocks
Standard Open Modelica Blocks
GUI Builder (“guibuilder”)
A Graphic User Interface Builder
Plotting library (“plotlib”)
Matlab-like Plotting library
Time Frequency Toolbox (“stftb”)
Toolbox developed for the analysis of non-stationary signals using time-frequency distributions.
CNES Space Mechanics Toolbox for Mission Analysis
Figure 18: The 10 most downloaded ATOMS modules.
There are two ways to install an ATOMS module. The ﬁrst way is to use the atomsGui function, which opens a Graphical User Interface (GUI) which lets the user browse through all the available ATOMS modules. This tool is also available from the menu ”Applications > Module manager – ATOMS” of the Scilab console. Within the GUI, we can read the description of the module and simply click on the ”Install”button. The ﬁgure 19 presents the ATOMS GUI.
The second way is to use the atomsInstall function, which takes the name of a module as input argument. For example, to install the makematrix module, the following statement should be executed:
Then Scilab should be restarted and the makematrix module (and, if any, its dependencies) are automatically loaded.
Exercise 2.1 (The console) Type the following statement in the console.
Now type on the <Tab> key. What happens? Now type the ”I”letter, and type again on <Tab>. What happens?
Exercise 2.2 (Using exec) When we develop a Scilab script, we often use the exec function in combination with the ls function, which displays the list of ﬁles and directories in the current directory. We can also use the pwd, which displays the current directory. The SCI variable contains the name of the directory of the current Scilab installation. We use it very often to execute the scripts which are provided in Scilab. Type the following statements in the console and see what happens.
pwd SCI ls (SCI+"/modules") ls (SCI+"/modules/graphics/demos") ls (SCI+"/modules/graphics/demos/2d_3d_plots") dname = SCI+"/modules/graphics/demos/2d_3d_plots"; filename = fullfile(dname ,"contourf.dem.sce"); exec(filename) exec(filename);
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.