Session 3: Workspace and Current Working Directory
Objectives
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:
- Commands to explore the Workspace
- Commands to manipulate the Workspace - save, clar and load variables
- Current Working Directory, its importance, querying its current setting, changing it to another directory
Commands to Work with the Scilab Workspace
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:
- who: Displays the names of items in the Workspace
- whos: Displays a detailed list of items in the Workspace, including their name, type, size (rows and columns) and memory used (in bytes)
- whos -name pattern: Same as whos, but filters the names of objects according to the specified pattern
- whos -type objtype: Same as whos, but filters the names of objects based on the specified objtype.
- save: Save variables in the Workspace to a file on disk. Usage -->save(filename) saves all user define variables in the workspace to the file specified by the string filename. For further explanation about the file, see the next discussion in this session.
- load: Load variables from a file on disk to Workspace
- clear: Erase variables from the Workspace
Important Note
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
Examples
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:
- You can only filter based on one of the above criteria. It is an error to specify both criteria simultaneously.
- Filter pattern for object names mathches only the beginning of the name. Wildcard patterns are not possible (for example * or ? used in matching filenames in bash shell)
Screencast
Watch the screencast on YouTube. (There is no audio. Read the comments. No need to type the comments when you try out the commands)
Current Working Directory
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.
What is 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.
Displaying 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. Even though both display the Current Working Directory, the type of 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.
Screencast
Watch the screencast on YouTube. (No audio. Read the comments. No need to type the comments when you try out the commands)
Changing the Current Working Directory
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.
Screencast
Watch the screencast on YouTube (No audio. Read the comments. No need to type the comments when you try out the commands)
Where Does Scilab Save My Files?
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.
From Where Does Scilab Read My Files?
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.
Saving Contents of the Workspace
Saving all variables to a file in the Current Working Directory
-->save('test.dat')
Saving all variables to a file in a directory of your choice
-->save('D:\Scilab\Project\test.dat') // These are comments. No need to type them. Windows operating system
-->save('~/prj/test.dat') // *nix operating systems
Saving selected variables to a file in the Current Working Directory
-->save('test.dat', x, y, z) // x, y and z are the variables you wish to save to the file
Saving selected variables to a file in a directory of your choice
-->save('D:\Scilab\Project\test.dat', x, y, z)
-->save('~/prj/test.dat', x, y, z)
Loading variables from a file into the Workspace
-->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
Clearing Variables from the Workspace
Clearing selected variables from the Workspace
-->clear x y z // clear only selected variables. NOTE NO COMMAS
Clearing all variables from the Workspace
-->clear // Clears all variables! Dangerous! Be sure you want to do this
Demonstrate save(), clear and load() work
-->// Create some variables and initialize them
-->a = 10; b = rand(3,4) * 100; c = sin(b);
-->//Verify that they exist in the Workspace
-->a, b, c
-->// Take note of your current working directory
-->pwd
-->// Save variables a, b and c to disk file test.dat in current working directory
-->save('test.dat', a, b, c)
-->// Kill variables a nd b. NOTE, NO COMMA
-->clear a b
-->// Display variable a. ERROR, no variable a in workspace
-->a
-->// Ditto, variable b
-->b
-->// We didn't kill variable c. It will be displayed
-->c
-->// Kill variable c
-->clear c
-->// ERROR
-->c
-->// Load variables a, b and c from disk file test.dat in current working directory
-->load('test.dat')
-->// Whew, back to normal again
-->a, b, c
Important Notes
- In the clear command, do not use comma to separate names of variables to be killed, separate them by whitespace. Comma is interpreted as end of statement, not as a separator.
- It is a good habit to always note the Current Working Directory as soon as you start a Scilab session, and change it to whatever you want right in the beginning.
- You can customize the Current Working Directory. See the section below.
Screencast
Watch the screencast on YouTube. (No audio. Read the comments. No need to type the comments when you try the commands)
Customizing Scilab using Startup File
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:
- Location of the startup file and its name
- The commands you wish to put in this startup file
Location and name of startup file
- The location of the startup file is defined in the Scilab Environment Variable SCIHOME. Display its value in the Scilab console and note it down. On Windows 7, it is usually C:\Users\<Username>\AppData\Roaming\Scilab\scilab-<version>. It will be different on Windows XP, but SCIHOME will correctly tell you what this directory is. On *nix it is usually /home/<user>
- Name of the startup file is .scilab.ini (Windows) or scilab (*nix, don't forget the . at the start)
Commands to put in the startup file
- Your Current Working Directory. For example cd('C:\Users\Satish\Scilab');
- Any constants or variables you want to use in every Scilab session
- Anything else you wish to customze