In this article we will learn how to use the general purpose Scilab functions. These functions are basic and they can be used whatever the scope of the work (numerical analysis, graphics, GUIs, etc.)
First we’ll make a table in which we’ll list all the general purpose functions and after we’ll take each of them and describe it in detail:
|Returns the size of a variable|
|List the names of all the variables defined in Scilab|
|Deletes variables within the workspace|
|List the name, type, size and bytes (memory) of all the variables defined in Scilab|
|Verifies if a variable exists or not in the workspace|
|Returns the type of the variable|
|On-line help command|
|Searches keywords in Scilab help|
|Clears Scilab console|
|Get files list|
|Changes Scilab current directory|
|Ends the current Scilab session|
There are three different ways to call a function. If the function doesn’t have arguments, like some the general purpose ones, we can call the function as:
Either way the result is the same. Example:
-->clc -->clc() -->clc();
A good practice is to use round brackets at the end of the function just to make a difference between the function itself and a common word. For example: when speaking about measuring the size of a matrix we use the Scilab function
In order to start using the above functions let’s define some variables to work with. As explained in the previous articles, we’ll create variables of type: real, complex, matrix, string, Boolean and polynomial. In order to define at the Scilab console multiple variables at once, we separate their definition with
-->var_real=14, var_complex=2+3*%i, var_matrix=rand(2,3), var_string="This is a string!", var_boolean=%T, var_poly=poly([-2 5 -4 1],"x","coeffs") var_real = 14. var_complex = 2. + 3.i var_matrix = 0.8782165 0.5608486 0.7263507 0.0683740 0.6623569 0.1985144 var_string = This is a string! var_boolean = T var_poly = 2 3 - 2 + 5x - 4x + x -->
If you want to check, or use in other calculations, the size of a variable, the
size() function does the job:
-->size(var_matrix) ans = 2. 3. -->
If applied to a matrix the number of rows and columns is returned. If we want to assign the number of rows and columns to variables
no_cols we can use this command:
-->[no_rows no_cols] = size(var_matrix) no_cols = 3. no_rows = 2. -->
If we want to check all the variables that are defined in the workspace the
who() function can be used. This functions lists all the variables, local, global and those used internally by Scilab.
If called with the arguments “local” or “global” the function will return only the variables with scope local or global.
If you want to store the list of variables into another variable, for example
var_global, this command can be used:
-->var_global=who("global") var_global = !%modalWarning ! ! ! !demolist ! ! ! !%driverName ! ! ! !%exportFileName ! ! ! !%helps ! ! ! !SCI2CHOME ! ! ! !slFuncs ! ! ! !%toolboxes ! ! ! !%toolboxes_dir ! -->
var_global will be a matrix of strings containing all the Scilab defined global variables.
If you want to delete a variable the
clear() function can be used. Used without any arguments this function will delete all the variables defined by the user. If you want to delete a specific variable write its name after the clear function:
-->clear var_poly -->var_poly !--error 4 Undefined variable: var_poly -->
This function is similar with
who() but it outputs more data related to the variables. If you call the function
whos() without any argument you’ll get a list of all the variables with their name, type, size and bytes (occupied in the memory):
-->whos -name var_ Name Type Size Bytes var_boolean boolean 1 by 1 24 var_complex constant 1 by 1 32 var_matrix constant 2 by 3 64 var_poly polynomial 1 by 1 72 var_real constant 1 by 1 24 var_string string 1 by 1 96 -->
whos() function can be used also with a filter. In the example above I’ve filtered in order to see only the variables defined with the
var_ prefix in the name.
exist() functions checks if a variable is already defined in the Scilab workspace. I order to call this function we have to enter the name of the variable as argument of the function:
-->exists("var_matrix") ans = 1. -->exists("var_temp") ans = 0. -->
If the variable exists the function will return
1 if not it will return
type() function returns an integer for each type of Scilab variable. The coding of the integer is explained in the following table:
|1||real or complex constant matrix|
|6||sparse boolean matrix|
|7||Matlab sparse matrix|
|8||matrix of integers stored on 1 2 or 4 bytes|
|9||matrix of graphic handles|
|10||matrix of character strings|
|11||un-compiled function (Scilab code)|
|13||compiled function (Scilab code)|
|16||typed list (tlist)|
|17||matrix oriented typed list (mlist)|
|128||pointer (See lufact)|
|128||size implicit polynomial used for indexing|
|130||Scilab intrinsic (C or Fortran code)|
In order to check the type of a specific variable its name must be feed as argument to the
-->type(var_boolean) ans = 4. -->type(var_poly) ans = 2. -->
In order to access the Scilab help files for a function you can use the
help() function. In the Scilab console write the
help keyword followed by the function name for which you want to get some help:
-->help type -->
You can use the function also as argument for the
These function will open the HTML based help description for each function. If the Help window is already opened Scilab will open the requested help information for the function.
If you need some information regarding a topic which is not a specific Scilab function you can use the
apropos() function. The usage of this function is similar with
help() function. You can write your keyword after the apropos function or as a argument:
-->apropos equation -->
This command will search the keyword “equation” in all the Scilab help files and will list them in the Find window of the Help Browser.
If you need some information about Cauchy integrals you can use them as argument for the
apropos() function. It will output all the help files that contain information about Cauchy.
The Scilab console displays the current entered instructions, functions or variables and also the history of previous used commands. In order to clear all the content of the Scilab Console we can use the
This clear function comes in handy when you write a Scilab script and want to see only the messages related to you script. In this case at the beginning of the script you put the
clc() function which will erase all the text within Scilab console when the script is executed.
In Scilab the current directory is the place on your hard drive where the file are saved by default. It is also the workspace. In order to check the contents of the current Scilab directory we can use the
-->dir ans = scilab_sce_script.sce scilab_sci_script.sci -->
If you want to save the content of the current directory into a Scilab variable you can use this command:
-->dir_content = dir dir_content = [L]=dir_content(str) -->
By default the Scilab’s current directory (the workspace folder) is the installation folder. Usually, under Windows, the path is “C:\Program Files\scilab-x.x.x\”. From the user point of view it is better to change the default current directory with a new, custom one. This can be done with the
-->cd("c:\Documents and Settings\All Users\ScilabWork\") ans = c:\Documents and Settings\All Users\ScilabWork -->
With this command we have changed the default current directory “c:\Program Files\scilab-x.x.x\” with “c:\Documents and Settings\All Users\ScilabWork”.
In order to exit Scilab you can type
exit in the Scilab console and press <ENTER>. Scilab will close.
In this article we only went through a couple of most common Scilab functions. There are a vast diversity of Scilab functions for the user to explore. Open the Scilab help and start learning as much as possible about the predefined functions. You never know when they’ll be needed.
For any questions, observations and queries regarding Scilab variables use the comment form below.
Don’t forget to Like, Share and Subscribe!