This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Getting Started with Scilab

Session 3: Workspace and Current Working Directory

Satish Annigeri's picture
Sat, 2011-05-21 09:36

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:

  1. Commands to explore the Workspace
  2. Commands to manipulate the Workspace - save, clar and load variables
  3. 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:

  1. who: Displays the names of items in the Workspace
  2. whos: Displays a detailed list of items in the Workspace, including their name, type, size (rows and columns) and memory used (in bytes)
  3. whos -name pattern: Same as whos, but filters the names of objects according to the specified pattern
  4. whos -type objtype: Same as whos, but filters the names of objects based on the specified objtype.
  5. 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.
  6. load: Load variables from a file on disk to Workspace
  7. 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:

  1. You can only filter based on one of the above criteria. It is an error to specify both criteria simultaneously.
  2. 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)

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. 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.

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.

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

Clearing all variables from the Workspace

-->clear // Dangerous! Be sure you want to do this

Demonstrate save(), clear and load() work

-->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

  1. 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.
  2. 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.
  3. You can customize the Current Working Directory. See the section below.

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:

  1. Location of the startup file and its name
  2. The commands you wish to put in this startup file

Location and name of startup file

  1. 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>
  2. 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

  1. Your Current Working Directory. For example cd('C:\Users\Satish\Scilab');
  2. Any constants or variables you want to use in every Scilab session
  3. Anything else you wish to customze