%-------------------------------------------------------------------------------
% lecture12
%-------------------------------------------------------------------------------
% Beginnings of object oriented programming (oop)
% + want to model multiple and related info
% + Tedious way: use many variables

% ex) model people
      fn1 = 'gene';
      ln1 = 'demaitre';
      id1 = 1234;

      fn2 = 'ben';
      ln2 = 'stein';
      id2 = 4321;

      % assume even more people!
    
% + Notes:
%   - different types causes weird behavior:
%     A = [1 'aa'] 
%     outputs just $aa$ (weird)
%   - even if use all strings, strings diff lengths (each char = 1 col)
%     A = ['a' 'bb' ; 'ccc' 'd']
%     causes error message because row 1 needs 3 cols and row 2 needs 4 cols

% + Want to:
%   - reduce redundant code
%   - clearly model "multiple" constructs (many parts make whole)

% + But how? use
%   - cell arrays
%   - structure arrays
% + exploring these leads into OOP!
%-------------------------------------------------------------------------------
% CELL ARRAYS (CAs)
% =================
% General:
% + most "general" MATLAB data structure
% + special kind of array
% + holds arrays (including CAs) inside elements   
% + arrays may be different types!
% + no math operations allowed (ML would get too confused)
% + cells called CONTAINERS
%-------------------------------------------------------------------------------
% Creating CAs:
% + Preallocation with $cell$
% + using {} notation
%-------------------------------------------------------------------------------
% Creating CA: Preallocation:
  help cell

% Portion of help file:
% CELL  Create cell array.
%    CELL(N) is an N-by-N cell array of empty matrices. 
%    CELL(M,N) or CELL([M,N]) is an M-by-N cell array of empty
%    matrices.

% ex) create an empty 2x2 cell array
      cell(2)
%     gives 4 empty matrices:
%     ans = 
%
%          []     []
%          []     []
%-------------------------------------------------------------------------------
% Creating CA: Using {} notation
% + three ways:
%   - using {} directly:  {row stuff ; more row stuff ; etc }
%   - cell indexing:      array(indicies) = {stuff}
%   - content indexing:   array{indicies} = stuff
% + all methods identical for results!

%   Direct
%   ======
    A = {'ben' 'stein' ; 4321 [] }
%   A = 
%
%      'ben'     'stein'
%       [4321]         []

%   Cell indexing
%   =============
    A(1,1) = {'ben'}; 
    A(1,2) = {'stein'};
    A(2,1) = {4321};
    A(2,2) = {[]};
    A
%   A = 
%
%      'ben'     'stein'
%       [4321]         []

%   Content indexing
%   ================
    A{1,1} = 'ben'; 
    A{1,2} = 'stein';
    A{2,1} = 4321;
    A{2,2} = [];
    A
%   A = 
%
%      'ben'     'stein'
%       [4321]         []

% See? Same results
% Related ops:
% - extending cell arrays: see Chapman p264 (ex: B=A; B{3,3} = 'hello')
% - deleting elements: see Chapman 266 (ex: B(1,:) = [] deletes 1st row)
% - reshape: see $help reshape$ (ex: reshape(A,1,4))
% - combining: ex) C = [A A] creates a new cell array with twice A
%-------------------------------------------------------------------------------
% CA: Displaying all contents
% + entering variable (see above examples)
    A

% + $celldisp$
    celldisp(A)            
%   A{1,1} =
%            ben
%   A{2,1} =
%            4321
%   A{1,2} =
%            stein
%   A{2,2} = 
%            []

% + $cellplot$
%   boxes indicate the number of items in each cell
    cellplot(A)
%-------------------------------------------------------------------------------
% CA: Displaying some contents

% () -> refers to contents that a cell points to
%    -> () index the cells directly
% {} -> refers to the actual contents pointed to by a cell
%    -> {} retrieve the data directly

  ex) B={'ira!' [1 2;3 4])

  B(2) % gives $[2x2 double]$
       % $B(2)$ is really a POINTER to a data structure
       % The pointer is an address that gives the location in computer memory 
       % for the contents referred to by the cell.

  B(1), B(2)  % could also just enter $B$

% ans = 
%    'ira'
% ans = 
%    [2x2 double]

  B{2} % gives [1 2;3 4]
       % $B(2)$ extracts the CONTENTS of the data structure
       % referred to by $B(2)$

  B{1}, B{2}

% ans =
%     'ira'
% ans =
%     1     2
%     3     4

% Notes:
% - individual values and strings are extracted in both cases
%   (because they are scalars)
% - extract individual elements of a structure inside a cell
%   ex) B{2}(1:4)
%       ans = 1 3 2 4
%       (pretty cool, huh?)
%-------------------------------------------------------------------------------
% Application of CA: cell array of strings
% + strings are arrays of characters
% + storing unequal length strings in array sickens ML
% + you could pad strings with blanks, but...
% + Cell arrays to the rescue!

  S{1} = 'I';
  S{2} = 'love';
  S{3} = 'MATLAB!';
  S
% S = 
%    'I'    'love'    'MATLAB!'

% Shortcuts:
% + $help cellstr$
% + S = {'I'    'love'    'MATLAB!};

% Related functions:
% $iscellstr$, $ischar$
%-------------------------------------------------------------------------------
