Matlab_03c

Variables that are mixed

More complicated data can be held in mixed type cell arrays:

% create cell array

my_tup = {'Martin',63,'Programmer'}

The elements can but accessed but you must use curly braces:

% access each one

my_name = my_tup{1};

my_age = my_tup{2};

years_ago = my_tup{2}-10;


This idea generalizes smoothly to more dimensions:

% create array of cell arrays

my_db{1} = {'Martin',63,'Programmer'};

my_db{2} = {'Freda',60,'Driver'};

my_db{3} = {'Suki',50,'Teacher'};


% access just one

my_db{3}{3}


But a more comfortable way to handle this, maybe, is with structure arrays:

% same as previous example but with structure arrays

per(1).name = 'Martin';

per(1).age = 63;

per(1).job = 'Programmer';


per(2).name = 'Freda';

per(2).age = 60;

per(2).job = 'Driver';


per(3).name = 'Suki';

per(3).age = 50;

per(3).job = 'Teacher';


per(3).job



Structure arrays use named fields and round brackets.