Page 96 - Basics of MATLAB and Beyond
P. 96

>> whos
                                 Name       Size         Bytes  Class
                                 t          4x1            658  cell array
                               Grand total is 149 elements using 658 bytes
                               >> t(1)
                               ans =
                                   ’O sacred receptacle of my joys,’
                               >> t{1}
                               ans  =
                               O sacred receptacle of my joys,
                               >> t{1}(1)
                               ans  =
                               O
                               >> t{1}(1:8)
                               ans  =
                               O sacred
                               Let us add another element to the cell array by putting a 3 × 3 matrix
                               in the first row of the second column:
                               >> t{1,2} = spiral(3)
                               t=
                                   [1x31 char]     [3x3 double]
                                   [1x34 char]               []
                                   [1x41 char]               []
                                   [1x39 char]               []
                               matlab has filled the rest of the cells in column 2 with empty cells. We
                               used the curly brackets t{1,2} to refer to that particular cell. If we had
                               used ordinary round brackets, we would have produced an error:

                               >> t(1,2) = spiral(3)
                               ??? Conversion to cell from double is not possible.
                               This is because there is a difference between indexing cells and indexing
                               their contents. For example, to extract the word “virtue” from the second
                               line of the quotation in the first column, we need to access the cell {2,1},
                               then get characters 15 to 20 from that cell’s contents:
                               >> t{2,1}(15:20)
                               ans  =
                               virtue
                               When assigning a cell you can use the curly brackets on either the left or
                               right hand side of the equals sign, but you must put them somewhere,to
                               tell matlab that you want this to be a cell. Otherwise, matlab thinks
                               you are defining a mathematical matrix and gives you an error to the
                               effect that the things on each side of the equal sign have different sizes.
                               For example, we can type:



                               c   2000 by CRC Press LLC
   91   92   93   94   95   96   97   98   99   100   101