Structure Arrays - Untitled
Description: Untitled

Next: Creating Structures, Up: Data Structures

#### 6.1.1 Structure Arrays

A structure array is a particular instance of a structure, where each of the fields of the structure is represented by a cell array. Each of these cell arrays has the same dimensions. An example of the creation of a structure array is

         x(1).a = "string1"
         x(2).a = "string2"
         x(1).b = 1
         x(2).b = 2

which creates a 2-by-1 structure array with two fields. As previously, to print the value of the structure array, you can type its name:

         octave:2> x
         x =
         {
           a =

           (,
             [1] = string1
             [2] = string2
           ,)

           b =

           (,
             [1] =  1
             [2] =  2
           ,)

         }

Individual elements of the structure array can be returned by indexing the variable like x`(1)`, which returns a structure with the two fields like

         octave:2> x(1)
         ans =
         {
           a = string1
           b =  1
         }

Furthermore, the structure array can return a comma separated list (see Comma Separated Lists), if indexed by one of itself field names. For example

         octave:3> x.a
         ans =

         (,
           [1] = string1
           [2] = string2
         ,)

The function `size` with return the size of the structure. For the example above

         octave:4> size(x)
         ans =

            1   2

Elements can be deleted from a structure array in a similar manner to a numerical array, by assigning the elements to an empty matrix. For example

         in = struct ("call1", {x, Inf, "last"},
                      "call2", {x, Inf, "first"});
         in (1, :) = []
         => in =
               {
                 call1 =

                 (,
                   [1] = Inf
                   [2] = last
                 ,)

                 call2 =

                 (,
                   [1] = Inf
                   [2] = first
                 ,)

               }