This is the P2PU Archive. If you want the current site, go to www.p2pu.org!
Once you have done a few calculations in the Scilab console, you are now ready to explore the invisible environment of Scilab, its Workspace. Learning to explore and manipulate the Workspace is necessary as it gives you an insight into how Scilab works on the inside.
In this session you will learn the following:
Before Scilab introduced the Variable Browser environment (Scilab versions up to 4), the only way to inspect and manipulate the Scilab Workspace was through the set of commands dedicated for that purpose. However, starting with Scilab version 5, Variable Browser provides a visual interface to examine and change Scilab Workspace and manipulate the values of the variables stored in the Workspace. Currently, the Variable Browser only shows Scilab Environment Variables, constants and variables. Functions and libraries loaded in the Workspace are not displayed. Further, while it is possible to change the values of the variables, it is not possible to clear the variables from the Workspace. Thus, knowing Scilab commands continues to be an advantage since the Variable Browser still lacks some features.
The commands that we will study in this session are:
Comments in Scilab console or Scilab script files are initiated with two forwards slashes. Comments last up to the end of the line where they begin. Therefore, if you see a comment in the commands described below, they need not be typed. They are only to explain the purpose of the command, or what to expect from the output. While you need not type them, reading and understanding them is very helpful in learning.
Here is an example:
-->SCIHOME // Displays Scilab Home Directory
Display the entire content of the Workspace
-->who
The above command lists only names of objects in the Workspace. Their type, siz and memory used are not listed. Filtering the output based on name is not possible.
Display detailed information about the contents of the Workspace
-->whos
The above command lists all the objects in the Workspace and displays the name, type, size and memory used by each object.
Filter the detailed list based on names of objects
-->whos -name a
The above command displays a detailed lists of only those objects in the Workspace whose name begins with the letter a.
Filter the detailed list based on type of objects
-->whos -type constant
The above command displays a detailed list of only those objects whose type is constant. To find the other types available in Scilab, search Scilab Help for typeof. Some other types available in Scilab are polynomial, function, handle, string, boolean, list, rational, library etc.
Important Notes:
When Scilab starts, it sets your Current Working Directory. The directory set as your working directory may depend on your operating system. But you can always enquire and find what your Current Working Directory is. Not just that, you can change your Current Working Directory to any directory you want. The importance of the Current Working Directory is that it is the default location from where Scilab will read files from (or write files to) when the filename you specify during readin (or writing) is not an absolute path.
On Windows operating system, a path starting with the drive letter is an absolute path. Thus C:\Users\Satish\My Documents\Scilab\test.sce is an absolute path specification because it begins with the drive letter C:. test.sce is the file name and C:\Users\Satish\My Documents\Scilab\ is the path. This path specification is an absolute path specification. On the otherhand, test.sce, ../examples/test.sce are relative path specifications.
On *nix operating systems, an absolute path specification begins with the root of the file system, namely the forward slash (/). Thus, /home/satish/Documents/Scilab/test.sce is an absolute path specifications whereas test.sce or ../examples/test.sce are relative path specifications.
Relative path specifications specify the location of a file relative to the Current Working Directory.
-->pwd -->pwd()
Both the above commands display the Current Working Directory being used by Scilab at that instant of time. The first is a function pointer and the second is a function. Both display the Current Working Directory, but the type of the return values are different. You can test it with the following commands:
-->typeof(pwd) ans = fptr -->typeof(pwd()) ans = string -->WSCI == pwd() ans = T -->WSCI == pwd !--error 144
WSCI is a constant of type string whose value is the directory where Scilab is installed. pwd() returns a string and hence the boolean comparison is possible. The result may be either T or F depending on the values of WSCI and pwd(). But pwd is of fptr and therefore the boolean comparison results in an error.
You can change the Current Working Directory used by the Scilab session with the commands cd or chdir. Alternately, you can use the Main Menu File --> Change current directory visually using a dialog box.
On Windows operating System use:
-->cd('C:\Users\Satish\My Documents\Scilab')
-->chdir('C:\Users\Satish\My Documents\Scilab')
On *nix operating systems use:
-->cd('/home/satish/Documents/scilab')
-->chdir('/home/satish/Documents/scilab')
Changing the Current Working Directory is much easier, especially for a beginner and when the directory path is very long if you use the dialog box available in the Main Menu File --> Change current directory.
If you specify the absolute path to the file, it will be saved indicated by your path specification. If you specify the relative path to the file, it will be saved to the current working directory.
If you specify the absolute path to your file, it will be read from the location indicated by your path specification. If you specify the relative path to the file, it will be read from the Current Working Directory.
-->save('test.dat')
-->save('D:\Scilab\Project\test.dat') // These are comments. No need to type them. Windows operating system
-->save('~/prj/test.dat') // *nix operating systems
-->save('test.dat', x, y, z) // x, y and z are the variables you wish to save to the file
-->save('D:\Scilab\Project\test.dat', x, y, z)
-->save('~/prj/test.dat', x, y, z)
-->load('test.dat') // Be sure to set Current Working Directory before using this command -->load('C:\Users\Satish\My Documents\Scilab\test.dat') // Windows. Long path spec! -->load('~/Scilab/test.dat') // *nix
-->clear x, y, z
-->clear // Dangerous! Be sure you want to do this
-->a = 10; b = rand(3,4) * 100; c = sin(b); // Create some variables and initialize them -->a, b, c // Verify the variables exist in your Workspace. Output not shown here -->pwd // Make a note of your Current Working Directory. Change it now, if you wish -->save('test.dat', a, b, c) // Make a note of the file in which yu saved the variables, and its directory -->clear a b // NOTE: NO COMMA between a and b. Kill variables a and b from the Workspace -->a // You will see error 4 Undefined variable -->b // Same as above -->c // Value of variable c is displayed -->clear c // Kill variable c -->c // Error -->load('test.dat') // Read variables from file test.dat in your Current Working Directory -->a, b, c // Everything is alright again
Important Notes
Scilab searches for a startup file when it starts, and executes the commands in that file. So customize this startup file to your requirements (create it if does not exist). The things you should know to do this successfully are: