This course will become read-only in the near future. Tell us at if that is a problem.

Session 14: Data Structures [June 13, 2011, 2:31 a.m.]


In this session you will learn the following:

  1. User defined data structures in Scilab
  2. Creating and using struct, cell, list, tlist and mlist


User defined data structures offer the programmer the facility to tailor data suited to represent and program solutions to specific problems. Most data structures are an agglomeration of basic data types and previously defined user defined data types.

Scilab Data Types

Data types available in Scilab can be listed through the online help on type() and typeof(). The data types available in Scilab are:

Data Type Object Type Description
1 constant real or complex constant matrix
2 polynomial polynomial matrix
4 boolean boolean matrix
5 sparse sparse matrix
6 boolean sparse sparse boolean matrix
8 int8, int16 or int32 matrix of integers stored in 1, 2 or 4 bytes
9 handle matrix of graphic handles
10 string matrix of character strings
11   un-compiled function (Scilab code)
13 function compiled function (Scilab code)
14 library function library
15 list list
16 tlist typed list (tlist)
17 st or mlist structure or matrix oriented typed list (mlist)
128 pointer pointer
129 size implicit size implicit polynomial used for indexing
130 fptr Scilab intrinsic (C or Fortran code)

The following functions are used to determine data types:

  1. Function type(x) returns the type of a Scilab variable x.
  2. Function typeof(x) returns a string indicating the type of Scilab object x.

Data Structure - struct

struct is a user defined data structure. Programmer can define fields within the structure and each field can be any valid Scilab data type. Each field can thus contain any valid Scilab data and data in any field can be accessed using the dot convention, namely, object.field accesses field from object. Let us see an example:

-->s = struct('name', 'John Doe', 'courses', ['MA102', 'HS108', 'CV102'], 'marks', [85, 92, 79]);
-->type(s) // enquire data type
 ans  =
-->typeof(s) // enquire object type
 ans  =
-->fieldnames(s) // list field names
 ans  =
!name    !
!courses !
!marks   !
-->s // display data
  name: "John Doe"
  courses: ["MA102", "HS108", "CV102"]
  marks: [85, 92, 79]
--> // display field "name" of object "s"
 ans  =
  John Doe
--> // display field "course" of object "s"
 ans  =
  !MA102  HS108  CV102  !
-->s.marks // display field "marks" of object "s"
 ans  =
   85.  92.  79.

This is what happened:

  1. We created a struct object with the name s using the struct function. While creating this object, we defined the fieldnames name (and assigned it the value "John Doe"), courses (and assigned it a vector of string elements) and marks (and assigned it a vector of numerical elements).
  2. We enquired the type of the object using type() and typeof() functions.
  3. We displayed the field names in the object s using function fieldnames()
  4. We displayed the data contained in object s
  5. We displayed the individual fields in the object s using the object.fieldname syntax