%celldemo is a script for a very simple example of a cell array for the
%Matlab for PhD students course
% This script is part of the Matlab course for PhD Students 2010-2011:
% http://www.sinnesphysiologie.uni-oldenburg.de/50246.html
% This version was written by Jutta Kretzberg, 23.01.2011

%% make sure variables for cell arrays do not exist

clear

%% generate a 1x3 cell array A
A{1}=[1 2 3];           % creates a cell array A with one 
                        % cell containing the vector 

A{2}='A B C D E';       % adds a second cell to cell Array A 
                        % containing the string 

A{3}=false;             % adds a third cell containing the
                        % logical value

input('Pause 1: press Return to continue');

celldisp(A)             % display A


%% Add a second dimension
A{2,1}=9        % your cell array A now has dimensions 2x3
				% with cell {3} becoming cell {1,3} etc,
				% and cells {2,1} and {2,3} being empty 
				% matrices [ ].


celldisp(A)     % display A to show this. 

input('Pause 2: press Return to continue');

%% Address specific cells

a= A{1,1}(2)        % second element of the vector contained in A{1,1}


[a,b]=A{:,1}        % example for use of : notation to address several cells

[c,d]=deal(A{1,3},A{2,1}) 	% alternative way to address combinations of cells

input('Pause 3: press Return to continue');

%% make a cell array out of a matrix and add it to the cell array A

m=randn(3,5);                 % matrix of random numbers
c=num2cell(m);                % convert to cell array
celldisp(c)                   % display new cell array

input('Pause 4: press Return to continue');

A{2,2}=c;                     % insert new cell array into A
celldisp(A)                   % display A

input('Pause 5: press Return to continue');
%% Differnet way to generate a cell array:

D=cell(3,2)             % generate a new 3-by-2 cell array with empty matrices


%% Use cell indexing instead of content addressing:

A(1,3)={9}        % assign value 9 to cell at position (1,3)

E={m, 'bla', true, D}   % convenient way to combine variables of different 
                        % types in one cell array
                        
t1=E(3)         % Returns a cell array
t2=E{3}         % Returns the contents (logical value 1)                      