Page 99 - Basics of MATLAB and Beyond
P. 99

>> t = {’help’ spiral(3) ; eye(2) ’I’’m stuck’};
                                    >> tt = {t t ;t’ fliplr(t)};
                                    >> tt{2,2}{2,1}(5:9)
                                    ans  =
                                    stuck


                               28    Structures

                               Structures are arrays whose names have dot-separated parts. They can
                               be used to store information of different kinds together in a hierarchical
                               structure. Let us do a simple example:
                               >> staff.name = ’John Smith’
                               staff =
                                   name: ’John Smith’
                               >> staff.age = 43
                               staff =
                                   name: ’John Smith’
                                    age: 43
                               >> staff.favourites = [1 42 37]
                               staff =
                                              name: ’John Smith’
                                               age: 43
                                       favourites: [1 42 37]
                               We have created a structure called staff which is of size 1 × 1:

                               >> whos
                                 Name         Size         Bytes   Class
                                 staff        1x1            424   struct array
                               The staff structure has three fields: name, age, and favourites:
                               >> staff
                               staff =
                                            name: ’John Smith’
                                             age: 43
                                     favourites: [1 42 37]
                               To add another staff member’s data to this structure, add subscripts to
                               define a second element:
                               staff(2).name = ’Jane Smythe’;
                               staff(2).age = 30;
                               staff(2).favourites = [pi eps realmax realmin NaN Inf];
                               The sizes of the fields do not have to be the same for each element of the
                               structure. For example, Jane Smythe’s favourite vector contains more
                               elements than John Smith’s.



                               c   2000 by CRC Press LLC
   94   95   96   97   98   99   100   101   102   103   104