cellarrays ------------------------------------------------------------------------------- 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$ ----------------------------------------------------------------------------- Cell Arrays and Structure Arrays + Store info with $struct$ s = struct('colors',{'blue','red','yellow'}) s = 1x3 struct array with fields: colors + $struct2cell$ and $cell2struct$ help cell2struct CELL2STRUCT Convert cell array to structure array. S = CELL2STRUCT(C,FIELDS,DIM) converts the cell array C into the structure S by folding the dimension DIM of C into fields of S. SIZE(C,DIM) must match the number of field names in FIELDS. FIELDS can be a character array or a cell array of strings. Huh? -> the number of columns of C must match the number of elements of FIELDS -> FIELDS is going to be 1-D array A = {'ben' 'stein' ; 4321 []}; use $reshape$ (takes elements from cols) A = cell2struct(reshape(CA,1,4),{'fn','id','ln','blank'},2) SA = fn: 'ben' id: 4321 ln: 'stein' blank: [] Get lost? Check type: class(SA) % struct class(CA) % cell -------------------------------------------------------------------------------