%% This illustrates the functionality of the CAindex class
% Written by Will Jackson
% Copyright 2010 The MathWorks, Inc.
c = {'first','second'; 1,2; '3',4; 'b',5};
CELLARRAY = CAindex(c);
numel(CELLARRAY);
%% Testing subsref
CELLARRAY(:) % Same as c(:)
CELLARRAY(1,2) % Same as c(1,2);
CELLARRAY{1:end,1:2} % Same as c{1:end,1:2};
CELLARRAY{:,:,:} % Same as c{:,:,:};
% These is the same as saying CELLARRAY(:,x) where x is the number of the
% column which is headed by the given string.
CELLARRAY('first') % Same as c(2:end,1)
CELLARRAY('second') % Same as c(2:end,2)
% These is the same as saying CELLARRAY{:,x} where x is the number of the
% column which is headed by the given string.
CELLARRAY{'first'} % Same as c{2:end,1}
CELLARRAY{'second'} % Same as c{2:end,2}
CELLARRAY(2:4,1:2,1,:,:,1) % Demo crazier indexing
CELLARRAY(2:end,1:end,1,:,:,1) % Demo crazier indexing with () and end
CELLARRAY{2:end,1:end,1,:,:,1} % Demo crazier indexing with {} and end
CELLARRAY(:,end) = {'second',2,3,4}
%% Demonstrating subsasgn
CELLARRAY('first') = {1;3;6}
CELLARRAY('first') = [1;3;6] % Same as previous line
CELLARRAY('first') = [1 3 6] % Same as previous line
CELLARRAY{'first'} = {1;3;6} % Same as previous line
CELLARRAY{'first'} = [1;3;6] % Same as previous line
CELLARRAY{'first'} = [1 3 6] % Same as previous line
CELLARRAY(2:end, 2) = {2;4;7}
CELLARRAY{2,1} = 'This works!'
CELLARRAY{3,1} = 12345
% [CELLARRAY{2:end,2}] = deal(1,2,3) %This does not work and I'm not sure
% it is possible
%% Demonstrating transpose, ctranspose, vertcat, horzcat and size
c2 = {'one', 'two'; 17 + 1i, 'Tom'; 'Sam', 13 - 14i; 'Steve', 'Paul'};
A = CAindex(c2)
A.'
A'
[A; A; A]
[A A A]
size(A)
%% Simple Use case
d = CAindex;
%If a column hasn't been created, it will be created. Also, the data can
%be any size, but it will be resized into a column when input like this.
d('first') = rand(1,20)
d{'first'} = rand(1,20) %Same as previous line
%Doesn't matter if the number of elements is different than the rest of the
%columns
d('second') = rand(1,10)
d{'second'} = rand(1,10) %Same as previous line
%This will plot even if the number of elements in each column differ
plot(d);
%#ok<*NOPTS>