How to write a script in Scilab
Scilab provides a user-friendly approach to scriptwriting.
A script, at its most basic, is a sequence of commands that are recorded in a file. This tool is a significant time-saver as it allows you to run the same command sequence multiple times, in different Scilab sessions, without the need to rewrite them. Scilab recognises scripts through the ".sce" extension.
You'd be employing SciNotes, a built-in tool in Scilab, to create scripts.
Firstly, navigate to the 'Applications' option in the top menu, and select 'SciNotes' from the drop-down that appears.
Opening SciNotes
Upon selection, SciNotes will open in a new window where scripts can be written or edited.
Creating a new script
Initiating a new script is simple - click on 'File' and select 'New'.
Alternatively, using the shortcut keys CTRL+N or the 'New' icon in SciNotes will also lead you to the editor.
The editor provides a conducive environment to start writing your script.
Saving a script
After crafting the script, click on 'File' and then 'Save' to store your script in a file.
The shortcut CTRL+S will also save your script. On saving the script for the first time, you'll be prompted to give it a name. Upon entering the desired name, click 'Ok'.
The script file is then stored on your computer, making it accessible for future use.
Here are a few tips when naming your scripts: Avoid starting the filename with special characters or numerals. Shy away from using keywords. Be mindful of the case sensitivity in Scilab. For instance, "MyScript" and "myscript" are perceived as separate files. Finally, it's recommended to save all related files in a single folder for easier navigation.
Executing a script
Running the script is equally simple. From the top menu, select 'Execute', then 'Save and Execute'.
Poi seleziona la voce Save ad esegui.
F5, the function key, also serves as a shortcut to run scripts. Alternatively, you can type the script file's name within the exec() command on the command line and hit 'Enter'.
--> exec("nomescript.sce")
SciNotes then runs the script, displaying the results on the console.
Opening a script
To access a previously saved script, go to 'File' and then select 'Open'.
The shortcut CTRL+O also serves this purpose. Another method is to locate and click on the file name in the File Browser window.
Select your desired script file and click the 'OK' button.
SciNotes will then open the file, showcasing the script in the editor.
If you wish to edit the script, use the 'edit' command and the filename on the command line.
--> edit nomescript.sce
Unless you specify the ".sce" extension in the filename, Scilab defaults to the function format ".sci".Once the script is loaded in the editor, you can either modify it or execute it as per your requirements.
Scilab and file formats: .sce vs .sci
Scilab primarily uses two types of script files.
- Function files (.sci)
These contain user-defined functions. Scilab loads the file's content but does not execute it. - Execution files (.sce)
These contain both commands and user-defined functions. Scilab loads the file and executes it right away.
It's interesting to note that Scilab's approach to file formats is distinct from Matlab, which treats all script files as execution files, signified by the .m extension.